Magento 2: Add/Update CMS Page via Install/Upgrade Script Programmatically

This article shows how you can add or update CMS Page using Install or Upgrade Script of a module in Magento 2.

This code is useful when you need to auto add/edit cms page while installing or upgrading your custom module.

Add CMS Page from InstallData Setup Script

This will add cms page when you install your custom module for the first time.

Here, I am using my custom module named Chapagain_HelloWorld.

File: app/code/Chapagain/HelloWorld/Setup/InstallData.php


<?php

namespace Chapagain\HelloWorld\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    /**
     * @var \Magento\Cms\Model\PageFactory
     */
    private $_pageFactory;

    /**
     * InstallData constructor
     *
     * @param \Magento\Cms\Model\PageFactory $pageFactory
     */
    public function __construct(
        \Magento\Cms\Model\PageFactory $pageFactory
    )
    {
        $this->_pageFactory = $pageFactory;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @throws \Exception
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        // Load cms page by identifier
        $cmsPage = $this->_pageFactory->create()->load('test-page', 'identifier');

        // Create CMS Page
        if (!$cmsPage->getId()) {
            $cmsPageData = [
                'title' => 'Test Page',
                'identifier' => 'test-page',
                'content' => "<div class='block'>
                                <h2>My Test Page</h2>
                            </div>",
                'is_active' => 1,
                'stores' => [0],
                'sort_order' => 0
            ];

            $this->_pageFactory->create()->setData($cmsPageData)->save();
        }
    }
}

Update CMS Page from UpgradeData Setup Script

This will update cms page when you upgrade your custom module. You can also write the add cms page code in the UpgradeData class. In this example, I will be updating a cms page that’s already been created before.

Here, I am using my custom module named Chapagain_HelloWorld.

In the below code, you can see that I have checked for my module’s version 0.1.1. So, while upgrading the module to version 0.1.1, the below UpgradeData script will get executed.

File: app/code/Chapagain/HelloWorld/Setup/UpgradeData.php


<?php

namespace Chapagain\HelloWorld\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    const YOUR_STORE_ID = 1;

    /**
     * @var \Magento\Cms\Model\PageFactory
     */
    private $_pageFactory;

    /**
     * UpgradeData constructor
     * 
     * @param \Magento\Cms\Model\PageFactory $pageFactory
     */
    public function __construct(
        \Magento\Cms\Model\PageFactory $pageFactory
    )
    {
        $this->_pageFactory = $pageFactory;
    }

    /**
     * Upgrade data for the module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @throws \Exception
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        // run the code while upgrading module to version 0.1.1 
        if (version_compare($context->getVersion(), '0.1.1') < 0) {
            $cmsPage = $this->_pageFactory->create()->setStoreId(self::YOUR_STORE_ID)->load('test-page', 'identifier');
            
            $cmsPageData = [
                'title' => 'Test Page 2',
                'identifier' => 'test-page',
                'is_active' => 1,
                'stores' => [self::YOUR_STORE_ID],
                'sort_order' => 0,
                'page_layout' => '1column',
                'meta_title' => 'Test page meta title',
                'meta_description' => 'Test page description',
                'content' => "<div class='block'>
                                <h2>My Test Page 2</h2>
                            </div>",
            ];

            if (!$cmsPage->getId()) {
              $this->_pageFactory->create()->setData($cmsPageData)->save();
            } else {
                $cmsPage->setContent($cmsPageData['content'])->save();
            }
        }

        $setup->endSetup(); 
    }
}

Hope this helps. Thanks.