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 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 given identifier already exists on the database. Useful for some situations where you just to create a content if it don't exists 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 creating pages with new blocks simultaneously:

Clone this wiki locally