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 , bool $ importAll )
@@ -73,6 +77,26 @@ public function execute(array $types, ?array $identifiers, bool $importAll)
7377 }
7478 }
7579
80+ private function getStoreIds ($ storeCodes ): array
81+ {
82+ $ storeIds = [];
83+ if (is_array ($ storeCodes ) && count ($ storeCodes )) {
84+ foreach ($ storeCodes as $ storeCode ) {
85+ try {
86+ if ($ storeCode === DumpCmsDataService::STORE_SCOPE_ALL ) {
87+ $ storeCode = self ::STORE_SCOPE_ADMIN ;
88+ }
89+ $ store = $ this ->storeRepository ->get ($ storeCode );
90+ $ storeIds [] = $ store ->getId ();
91+ } catch (\Magento \Framework \Exception \NoSuchEntityException $ exception ) {
92+ echo $ exception ->getMessage () . "\n" ;
93+ }
94+ }
95+ }
96+
97+ return $ storeIds ;
98+ }
99+
76100 private function importBlocks (string $ dirPath , ?array $ identifiers ): void
77101 {
78102 $ filePaths = $ this ->directoryRead ->read ($ this ->varPath . $ dirPath );
@@ -83,6 +107,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
83107 }
84108 $ identifier = str_replace ($ dirPath , '' , $ filePath );
85109 $ identifier = str_replace ('.html ' , '' , $ identifier );
110+ $ identifier = substr_replace ($ identifier , '' , strpos ($ identifier , '| ' ));
86111 if ($ identifiers !== null && !in_array ($ identifier , $ identifiers )) {
87112 // If we have a list of items, we skip if its not in the list
88113 continue ;
@@ -102,11 +127,12 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
102127 'stores' => [1],
103128 'is_active' => $block->isActive()
104129 ];*/
130+ $ storeIds = $ this ->getStoreIds ($ jsonData ['stores ' ]);
105131 $ block ->setTitle ($ jsonData ['title ' ]);
106132 $ block ->setContent ($ content );
107133 $ block ->setIdentifier ($ jsonData ['identifier ' ]);
108- $ block ->setStoreId ($ jsonData ['stores ' ][0 ]);
109134 $ block ->setIsActive ((bool )$ jsonData ['is_active ' ]);
135+ $ block ->setStores ($ storeIds );
110136 if (isset ($ jsonData ['is_tailwindcss_jit_enabled ' ])) {
111137 $ block ->setIsTailwindcssJitEnabled ($ jsonData ['is_tailwindcss_jit_enabled ' ]);
112138 }
@@ -128,8 +154,9 @@ private function importPages(string $dirPath, ?array $identifiers): void
128154 continue ;
129155 }
130156 $ identifier = str_replace ($ dirPath , '' , $ filePath );
131- $ identifier = str_replace ('| ' , '/ ' , $ identifier );
132157 $ identifier = str_replace ('.html ' , '' , $ identifier );
158+ $ identifier = substr_replace ($ identifier , '' , strpos ($ identifier , '| ' ));
159+ $ identifier = str_replace ('| ' , '/ ' , $ identifier );
133160 $ identifier = str_replace ('_html ' , '.html ' , $ identifier );
134161 if ($ identifiers !== null && !in_array ($ identifier , $ identifiers )) {
135162 // If we have a list of items, we skip if its not in the list
@@ -144,6 +171,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
144171 $ content = $ this ->directoryRead ->readFile ($ filePath );
145172 $ jsonData = $ this ->directoryRead ->readFile (str_replace ('.html ' , '.json ' , $ filePath ));
146173 $ jsonData = $ this ->serializer ->unserialize ($ jsonData );
174+ $ storeIds = $ this ->getStoreIds ($ jsonData ['stores ' ]);
147175 /*$jsonContent = [
148176 'title' => $page->getTitle(),
149177 'is_active' => $page->isActive(),
@@ -157,6 +185,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
157185 $ page ->setPageLayout ($ jsonData ['page_layout ' ]);
158186 $ page ->setContentHeading ($ jsonData ['content_heading ' ]);
159187 $ page ->setIsActive ((bool )$ jsonData ['is_active ' ]);
188+ $ page ->setStores ($ storeIds );
160189 if (isset ($ jsonData ['is_tailwindcss_jit_enabled ' ])) {
161190 $ page ->setIsTailwindcssJitEnabled ($ jsonData ['is_tailwindcss_jit_enabled ' ]);
162191 }
0 commit comments