-
Notifications
You must be signed in to change notification settings - Fork 2
CMS Content
For CMS related stuff, we have both CmsPage
and CmsBlock
facades, for.. well, what their names imply.
To import them:
private Migration\Facade\CmsBlock $cmsBlock;
private Migration\Facade\CmsPage $cmsPage;
public function __construct(
Migration\Context $context,
Migration\Facade\CmsBlock $cmsBlock,
Migration\Facade\CmsPage $cmsPage
) {
parent::__construct($context);
$this->cmsBlock = $cmsBlock;
$this->cmsPage = $cmsPage;
}
Their share exactly the same methods, as described below:
Create a new entry on the database for the given content. Ex.:
$this->cmsPage->create('my-new-page', [
'title' => 'Lorem Ipsum',
'content' => <<<HTML
<span>Hello World!</span>
HTML,
]);
Update the content of existing cms content.
$this->cmsBlock->update('footer', [
'title' => 'Lorem Ipsum',
'content' => <<<HTML
<ul>// page builder generated stuff</ul>
HTML,
]);
Literally what the name implies, optionally restricting the scope to a single store.
$this->cmsBlock->delete('winter-offers-2022');
Check if a page/block with the given identifier already exists on the database. Useful for some situations where you just to create content if it doesn't exist yet.
if (!$this->cmsBlock->exists('winter-offers-2023')) {
$this->cmsBlock->create('winter-offers-2023', [...$data]);
}
Converts any given identifier to its respective id. Useful for handling pages that do refer to an existing block:
$faqBlockId = $this->cmsBlock->findId('faq-common');
$this->cmsPage->update('about', [
'content' => <<<HTML
<p>The content below comes from the cms block above:</p>
<div data-content-type="block" data-appearance="default" data-element="main">
{{widget
type="Magento\Cms\Block\Widget\Block"
template="widget/static_block/default.phtml"
block_id="$faqBlockId"
type_name="CMS Static Block"
}}
</div>
HTML,
]);
💡 Tip: use the page builder normally and copy the "content" field directly from your database to fill the html 'content'.
We want YOU for our community 🫵