We can easily create our custom CMS Block and CMS Pages Programaticaly from custom block as per below code.
Inside your custom block file add below code to create custom CMS Block or CMS Page
<?php namespace [VendorName]\[ModuleName]\Block\Adminhtml\Order; /** * Class CmsBox * * @package [VendorName]\[ModuleName]\Block\Adminhtml\Order
*/ use Magento\Cms\Model\PageFactory; class CmsBox extends \Magento\Backend\Block\Template { /** * Page factory. * * @var PageFactory */ private $pageFactory; private $blockFactory; /** * Constructor * * @param \Magento\Backend\Block\Template\Context $context * @param array $data */ public function __construct( PageFactory $pageFactory, \Magento\Cms\Model\BlockFactory $blockFactory, \Magento\Backend\Block\Template\Context $context, array $data = [] ) { $this->pageFactory = $pageFactory; $this->blockFactory = $blockFactory; parent::__construct($context, $data); } public function createBlock() // for creating CMS Block { $testBlock = [ 'title' => 'Test block title', 'identifier' => 'test-block', 'stores' => [0], 'is_active' => 1, ]; return $this->blockFactory->create()->setData($testBlock)->save(); } public function createPage() // for creating CMS Page { $testPage = [ 'title' => 'Test page title', 'identifier' => 'test-page', 'stores' => [0], 'is_active' => 1, 'content_heading' => 'Test page heading', 'content' => 'Test page content', 'page_layout' => '1column' ]; $this->pageFactory->create()->setData($testPage)->save(); } }
Tag :
Magento2,
Magento2 Extensions
0 Comments On "Magento2 Create Custom CMS Block and CMS Pages Programatically"