Introduction

In this chapter we will create CMS-Page programmatically in the Magento 2.

But first a few words about why DataPatch.

If you are developing something for your Magento 2 client which has staging and production envirenments, the best way to do changes for admin part (like changing settings, updating attributes or modifying CMS-Blocks and CMS-pages) is to create DataPatch.

DataPatch allows you to avoid remember what and where in the admin panel you did changes. And it’s guarrantie that chnages will be 100% applied and 100% the same on both (or more) environments after deployment.

Solution

<?php

namespace Vendor\ModuleName\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Cms\Model\PageFactory;

class CreateCmsPage implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * @var PageFactory
     */
    protected PageFactory $pageFactory;

    /**
     * @param PageFactory $pageFactory
     */
    public function __construct(
        PageFactory $pageFactory
    ) {
        $this->pageFactory = $pageFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $identifier = 'test-cms-page';

        $page = $this->pageFactory->create();

        $page->setTitle('Test CMS Page')
            ->setIdentifier($identifier)
            ->setIsActive(true)
            ->setPageLayout('1column')
            ->setStores([2])
            ->setContent('Oh My Gash! It\'s the programmatically created CMS-Page in the Magento 2.')
            ->save();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        /**
         * No dependencies for this
         */
        return [];
    }

    /**
     * Delete block
     */
    public function revert()
    {}

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

Explanation

First of all we create CMS-Page object:

$page = $this->pageFactory->create();

Then - put necessary params to it like identifier, title, content etc…

$page->setTitle('Test CMS Page')
    ->setIdentifier($identifier)
    ->setIsActive(true)
    ->setPageLayout('1column')
    ->setStores([2])
    ->setContent('Oh My Gash! It\'s the programmatically created CMS-Page in the Magento 2.')

And there is one interesting parameter - Stores.

The setStores method applies array of StoreView Ids - this is array of store views to which you want apply this page.

By default, if you don’t provide storeview(s) id or if you set [0], it will be applied to the all storeviews. magento-2-create-cms-page-all-storeviews

But, if you want to apply the page for specific storeview(s) - you should provide storeview(s) id.

$page->setStores([2]);

magento-2-create-cms-page-specific-storeviews

And the last step is to save CMS-Page

$page->save();

I hope this short article has helped you understand how to create CMS-Pages programmatically in the Magento 2.

✌️