Skip to content

Commit 70759f2

Browse files
author
Rocket Web
authored
Merge pull request #6 from rocketweb/ICP-9_translate_block_id
replacing cms block ID with block code [#9]
2 parents 95cf57d + 9c29780 commit 70759f2

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Model/Service/DumpCmsDataService.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DumpCmsDataService
3232
private \Magento\Catalog\Model\CategoryList $categoryList;
3333
private \Magento\Store\Model\StoreManagerInterface $storeManager;
3434
private array $blockIdentifiers = [];
35+
private array $blocksMapping = [];
3536

3637
public function __construct(
3738
\Magento\Cms\Api\PageRepositoryInterface $pageRepository,
@@ -80,6 +81,28 @@ private function write(WriteInterface $writeDirectory, string $filePath, string
8081
$stream->close();
8182
}
8283

84+
private function replaceBlockIds(string $content): string
85+
{
86+
preg_match_all('/block_id=\"([0-9]+)\"/', $content, $blockIds);
87+
if (isset($blockIds[1])) {
88+
$searchCriteria = $this->criteriaBuilder;
89+
$searchCriteria->addFilter('block_id', $blockIds[1], 'in');
90+
$blocksList = $this->blockRepository->getList($searchCriteria->create());
91+
$blocks = $blocksList->getItems();
92+
foreach ($blocks as $block) {
93+
if (!isset($this->blocksMapping[$block->getId()])) {
94+
$this->blocksMapping[$block->getId()] = $block->getIdentifier();
95+
}
96+
}
97+
foreach ($blockIds[1] as $blockId) {
98+
$identifier = $this->blocksMapping[$blockId];
99+
$content = str_replace("block_id=\"$blockId\"", "block_id=\"$identifier\"", $content);
100+
}
101+
}
102+
103+
return $content;
104+
}
105+
83106
private function getStoreCodes($stores): array
84107
{
85108
$storeCodes = [];
@@ -117,9 +140,11 @@ private function dumpPages(string $path, WriteInterface $varDirectory, ?array $i
117140
if (strpos($identifier, '.html') !== false) {
118141
$identifier = str_replace('.html', '_html', $identifier);
119142
}
143+
120144
$storeCodes = $this->getStoreCodes($page->getStores());
121145
$htmlPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.html';
122-
$this->write($varDirectory, $htmlPath, $page->getContent());
146+
$pageContent = $this->replaceBlockIds($page->getContent());
147+
$this->write($varDirectory, $htmlPath, $pageContent);
123148
$jsonPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.json';
124149
$jsonContent = [
125150
'title' => $page->getTitle(),

0 commit comments

Comments
 (0)