Magento 2 : Cms Page Installer

<?php

namespace Namespace\Cms\Setup;


use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;
use Magento\Cms\Model\PageRepository;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * @var PageRepository
     */
    private $pageRepository;
    /**
     * @var PageFactory
     */
    private $pageFactory;
    /**
     * InstallData constructor.
     * @param PageFactory $pageFactory
     * @param PageRepository $pageRepository
     */
    public function __construct(
        PageFactory $pageFactory,
        PageRepository $pageRepository
    ) {
        $this->pageRepository = $pageRepository;
        $this->pageFactory = $pageFactory;
    }
    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** Get your own Store Id **/
        $myStoreId = 2;
        /** @var Page $page */
        $page = $this->pageFactory->create();
        $page->setStoreId($myStoreId);
        $page->setContent(
            <<<HTML
<div>Lorem Ipsum ...etc </div>
<div>Lorem Ipsum ...etc </div>
HTML
        );

        $page->setIdentifier('my-custom-identifier');
        $page->setTitle('My custom Title');
        $page->setPageLayout('1column');
        /** @var PageRepository $pageRepository */
        $pageRepository = $this->pageRepository;
        $pageRepository->save($page);
    }
}

Name (required)Email (required)Website

Leave a Reply