Skip to content

CMS Content

Code Slicer edited this page Sep 12, 2022 · 3 revisions

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
) {
    $this->cmsBlock = $cmsBlock;
    $this->cmsPage = $cmsPage;
}

Their share exactly the same methods, as described below:

create($identifier, $data, $storeId = null)

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($identifier, $data, $storeId = null)

Update the content of existing cms content.

$this->cmsBlock->update('footer', [
    'title' => 'Lorem Ipsum',
    'content' => <<<HTML
        <ul>// page builder generated stuff</ul>
    HTML,
]);

delete($identifier, $storeId = null)

Literally what the name implies, optionally restricting the scope to a single store.

$this->cmsBlock->delete('winter-offers-2022');

exists($identifier, $storeId = null)

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]);
}

findId($identifier, $storeId = null)

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'.

Clone this wiki locally