2121
2222class ImportCmsDataService
2323{
24+ private const STORE_SCOPE_ADMIN = 'admin ' ;
2425 private \Magento \Cms \Api \PageRepositoryInterface $ pageRepository ;
2526 private \Magento \Cms \Api \BlockRepositoryInterface $ blockRepository ;
2627 private \Magento \Framework \Serialize \SerializerInterface $ serializer ;
2728 private \Magento \Framework \Filesystem \Directory \ReadInterface $ directoryRead ;
2829 private \Magento \Cms \Api \Data \BlockInterfaceFactory $ blockFactory ;
2930 private \Magento \Cms \Api \Data \PageInterfaceFactory $ pageFactory ;
31+ private \Magento \Store \Api \StoreRepositoryInterface $ storeRepository ;
3032 private string $ varPath ;
3133
3234 public function __construct (
@@ -36,7 +38,8 @@ public function __construct(
3638 \Magento \Cms \Api \Data \BlockInterfaceFactory $ blockFactory ,
3739 \Magento \Framework \Filesystem \DirectoryList $ directoryList ,
3840 \Magento \Framework \Filesystem $ filesystem ,
39- \Magento \Framework \Serialize \SerializerInterface $ serializer
41+ \Magento \Framework \Serialize \SerializerInterface $ serializer ,
42+ \Magento \Store \Api \StoreRepositoryInterface $ storeRepository
4043 ) {
4144 $ this ->pageRepository = $ pageRepository ;
4245 $ this ->blockRepository = $ blockRepository ;
@@ -45,6 +48,7 @@ public function __construct(
4548 $ this ->varPath = $ directoryList ->getPath (DirectoryList::VAR_DIR ) . '/ ' ;
4649 $ this ->blockFactory = $ blockFactory ;
4750 $ this ->pageFactory = $ pageFactory ;
51+ $ this ->storeRepository = $ storeRepository ;
4852 }
4953
5054 public function execute (array $ types , ?array $ identifiers )
@@ -69,6 +73,26 @@ public function execute(array $types, ?array $identifiers)
6973 }
7074 }
7175
76+ private function getStoreIds ($ storeCodes ): array
77+ {
78+ $ storeIds = [];
79+ if (is_array ($ storeCodes ) && count ($ storeCodes )) {
80+ foreach ($ storeCodes as $ storeCode ) {
81+ try {
82+ if ($ storeCode === DumpCmsDataService::STORE_SCOPE_ALL ) {
83+ $ storeCode = self ::STORE_SCOPE_ADMIN ;
84+ }
85+ $ store = $ this ->storeRepository ->get ($ storeCode );
86+ $ storeIds [] = $ store ->getId ();
87+ } catch (\Magento \Framework \Exception \NoSuchEntityException $ exception ) {
88+ echo $ exception ->getMessage () . "\n" ;
89+ }
90+ }
91+ }
92+
93+ return $ storeIds ;
94+ }
95+
7296 private function importBlocks (string $ dirPath , ?array $ identifiers ): void
7397 {
7498 $ filePaths = $ this ->directoryRead ->read ($ this ->varPath . $ dirPath );
@@ -79,6 +103,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
79103 }
80104 $ identifier = str_replace ($ dirPath , '' , $ filePath );
81105 $ identifier = str_replace ('.html ' , '' , $ identifier );
106+ $ identifier = substr_replace ($ identifier , '' , strpos ($ identifier , '| ' ));
82107 if ($ identifiers !== null && !in_array ($ identifier , $ identifiers )) {
83108 // If we have a list of items, we skip if its not in the list
84109 continue ;
@@ -98,12 +123,12 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
98123 'stores' => [1],
99124 'is_active' => $block->isActive()
100125 ];*/
126+ $ storeIds = $ this ->getStoreIds ($ jsonData ['stores ' ]);
101127 $ block ->setTitle ($ jsonData ['title ' ]);
102128 $ block ->setContent ($ content );
103129 $ block ->setIdentifier ($ jsonData ['identifier ' ]);
104- $ block ->setStoreId ($ jsonData ['stores ' ][0 ]);
105130 $ block ->setIsActive ((bool )$ jsonData ['is_active ' ]);
106-
131+ $ block -> setStores ( $ storeIds );
107132 try {
108133 $ this ->blockRepository ->save ($ block );
109134 } catch (\Exception $ exception ) {
@@ -121,8 +146,9 @@ private function importPages(string $dirPath, ?array $identifiers): void
121146 continue ;
122147 }
123148 $ identifier = str_replace ($ dirPath , '' , $ filePath );
124- $ identifier = str_replace ('| ' , '/ ' , $ identifier );
125149 $ identifier = str_replace ('.html ' , '' , $ identifier );
150+ $ identifier = substr_replace ($ identifier , '' , strpos ($ identifier , '| ' ));
151+ $ identifier = str_replace ('| ' , '/ ' , $ identifier );
126152 $ identifier = str_replace ('_html ' , '.html ' , $ identifier );
127153 if ($ identifiers !== null && !in_array ($ identifier , $ identifiers )) {
128154 // If we have a list of items, we skip if its not in the list
@@ -137,6 +163,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
137163 $ content = $ this ->directoryRead ->readFile ($ filePath );
138164 $ jsonData = $ this ->directoryRead ->readFile (str_replace ('.html ' , '.json ' , $ filePath ));
139165 $ jsonData = $ this ->serializer ->unserialize ($ jsonData );
166+ $ storeIds = $ this ->getStoreIds ($ jsonData ['stores ' ]);
140167 /*$jsonContent = [
141168 'title' => $page->getTitle(),
142169 'is_active' => $page->isActive(),
@@ -150,6 +177,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
150177 $ page ->setPageLayout ($ jsonData ['page_layout ' ]);
151178 $ page ->setContentHeading ($ jsonData ['content_heading ' ]);
152179 $ page ->setIsActive ((bool )$ jsonData ['is_active ' ]);
180+ $ page ->setStores ($ storeIds );
153181
154182 try {
155183 $ this ->pageRepository ->save ($ page );
0 commit comments