Create CMS Page By Installer In Magento 2 – Here I’m going to explain you how you can create cms page by patch data installer in magento2.
First of all create a “AddCustomCmsPage.php” file on location Vendor\Module\Setup\Patch\Data then use below code snippet.
<?php namespace Vendor\Module\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Cms\Model\PageFactory; class AddCustomCmsPage implements DataPatchInterface { /** * @param PageFactory $pageFactory */ public function __construct( PageFactory $pageFactory ) { $this->pageFactory = $pageFactory; } /** * @inheritdoc */ public function apply() { $cmsPageData = [ 'title' => 'Custom cms page', // cms page title 'page_layout' => '1column', // cms page layout 'meta_keywords' => 'Page keywords', // cms page meta keywords 'meta_description' => 'Page description', // cms page description 'identifier' => 'custom-page', // cms page url identifier 'content_heading' => 'Custom cms page', // Page heading 'content' => "<h1>Write your custom cms page content.......</h1>", // page content 'is_active' => 1, // define active status 'stores' => [0], // assign to stores 'sort_order' => 0 // page sort order ]; // create page $this->pageFactory->create()->setData($cmsPageData)->save(); } /** * @inheritdoc */ public static function getDependencies() { return []; } /** * @inheritdoc */ public function getAliases() { return []; } }
In this way we can create CMS page programmatically by installer file.Thanks..
$setup->startSetup();
if (version_compare($context->getVersion(), ‘1.0.0’) ‘Custom cms page’, // cms page title
‘page_layout’ => ‘1column’, // cms page layout
‘meta_keywords’ => ‘Page keywords’, // cms page meta keywords
‘meta_description’ => ‘Page description’, // cms page description
‘identifier’ => ‘custom-page’, // cms page url identifier
‘content_heading’ => ‘Custom cms page’, // Page heading
‘content’ => “Write your custom cms page content…….”, // page content
‘is_active’ => 1, // define active status
‘stores’ => [0], // assign to stores
‘sort_order’ => 0 // page sort order
];
// create page
$this->pageFactory->create()->setData($cmsPageData)->save();
}
$setup->endSetup();
Please update the code. This might help someone.