On this weblog, we are going to find out about saving Customized CMS block on the admin finish. We already know the right way to create the identical utilizing the setup/patch (https://webkul.com/weblog/create-cms-block-installer-magento-2/). On this method, we do need to run Magento instructions to register the identical, to beat with identical we are able to do the identical job through the use of the CMS mannequin i.e. “MagentoCmsModelBlockFactory”.
On this mannequin, we are able to use the perform save() so as to add the cms block programmatically.
Let’s examine the code on the right way to do the identical:
First, we are going to create a module by which we are going to create a controller to avoid wasting the CMS block for that we require knowledge to submit for that we are going to create a type through the use of the .phtml file.
<div> <type methodology="publish" motion="<?= $block->escapeUrl($block->getUrl('cmsblocksave/block/save')); ?>" enctype="multipart/form-data" data-mage-init='{"validation":{}}' id="wk-mpzcv-form"> <fieldset class="fieldset"> <?= $block->escapeHtml($block->getFormKeyBlockHtml()); ?> <legend class="legend"> <span><?= $block->escapeHtml(__("CMS Block")); ?></span> <button class="button wk-mp-btn" kind="submit"> <span><span><?= $block->escapeHtml(__("Save CMS Block")); ?></span></span> </button> </legend> <div class="area required"> <label for="mpzcv-region" class="label"> <span><?= $block->escapeHtml(__("Title")); ?></span> </label> <div class="management"> <enter kind="textual content" class="input-text required-entry validate-no-html-tags" title="<?= $block->escapeHtml(__('Title')); ?>" identify="title" id="mpzcv-region" aria-required="true"> </div> </div> <div class="area required"> <label for="mpzcv-region" class="label"> <span><?= $block->escapeHtml(__("identifier")); ?></span> </label> <div class="management"> <enter kind="textual content" class="input-text required-entry validate-no-html-tags" title="<?= $block->escapeHtml(__('identifier')); ?>" identify="identifier" id="mpzcv-region" aria-required="true"> </div> </div> <div class="area required"> <label for="mpzcv-zipcodes" class="label"> <span><?= $block->escapeHtml(__("is_active")); ?></span> </label> <div class="management"> <choose identify="is_active" class="required-entry"> <choice worth="1"> <?= $block->escapeHtml(__("Allow")); ?> </choice> <choice worth="0"> <?= $block->escapeHtml(__('Disable')); ?> </choice> </choose> </div> </div> <div class="area required"> <label for="mpzcv-zipcodes" class="label"> <span><?= $block->escapeHtml(__("is_active")); ?></span> </label> <div class="management"> <choose a number of = "a number of" identify="is_active" class="required-entry"> <choice worth="0">All Retailer Views </choice> <choice worth="1">Default Retailer View </choice> </choose> </div> </div> </fieldset> </type> </div>
Now we are going to create a controller to hit the phtml file. like Controller/Block/Add.php
<?php namespace WebkulCmsBlockSaveControllerBlock; use MagentoFrameworkAppActionAction; use MagentoFrameworkAppActionContext; use MagentoFrameworkViewResultPageFactory; use MagentoFrameworkAppRequestInterface; class Add extends Motion { /** * @var MagentoCustomerModelSession */ protected $_customerSession; /** * @var PageFactory */ protected $resultPageFactory; /** * @var MagentoCustomerModelUrl */ protected $url; /** * @param Context $context * @param PageFactory $resultPageFactory * @param MagentoCustomerModelSession $customerSession * @param MagentoCustomerModelUrl $url */ public perform __construct( Context $context, PageFactory $resultPageFactory, MagentoCustomerModelSession $customerSession, MagentoCustomerModelUrl $url, ) { $this->_customerSession = $customerSession; $this->resultPageFactory = $resultPageFactory; $this->url = $url; mother or father::__construct($context); } /** * Add Retailer Web page * * @return MagentoFrameworkViewResultPage */ public perform execute() { /** @var MagentoFrameworkViewResultPage $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->set(__('Add Cms Block')); return $resultPage; } }
to hit the controller now we have to create a format file: cmsblocksave_block_add.xml
<?xml model="1.0"?> <web page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Format/and so on/page_configuration.xsd"> <physique> <referenceContainer identify="content material"> <block class="WebkulCmsBlockSaveBlockCmsAdd" identify="cmsblocksave_block_add" template="Webkul_CmsBlockSave::add.phtml" cacheable="false"/> </referenceContainer> </physique> </web page>
Type will likely be like talked about beneath:

By this code we will present the shape to avoid wasting the customized CMS block on the admin finish.
Now we are going to examine the code to avoid wasting the cms block talked about beneath
<?php namespace WebkulCmsBlockSaveControllerBlock; use MagentoFrameworkAppActionAction; use MagentoFrameworkAppActionContext; use MagentoFrameworkViewResultPageFactory; use MagentoCmsModelBlockFactory; /** * Save CMS block motion. */ class Save extends Motion { /** * @var DataPersistorInterface */ protected $resultPageFactory; /** * @var BlockFactory */ personal $blockFactory; /** * @var BlockRepositoryInterface */ personal $coreRegistry; personal $jsonFactory; /** * @param Context $context * @param Registry $coreRegistry * @param DataPersistorInterface $dataPersistor * @param BlockFactory|null $blockFactory * @param BlockRepositoryInterface|null $blockRepository */ public perform __construct( Context $context, PageFactory $resultPageFactory, MagentoFrameworkRegistry $coreRegistry, MagentoFrameworkControllerResultJsonFactory $jsonFactory, BlockFactory $blockFactory ) { $this->resultPageFactory = $resultPageFactory; $this->coreRegistry = $coreRegistry; $this->jsonFactory = $jsonFactory; $this->blockFactory = $blockFactory; mother or father::__construct($context); } /** * Save motion * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return MagentoFrameworkControllerResultInterface */ public perform execute() { $testBlock = $this->getRequest()->getPostValue(); $this->blockFactory->create()->setData($testBlock)->save(); $returnData = ['status' => true, 'data' => $testBlock]; return $this->resultRedirectFactory->create()->setPath( 'cmsblocksave/block/add', ['_secure' => $this->getRequest()->isSecure()] ); return $this->jsonFactory->create()->setData($returnData); } }
Output will likely be.

That’s all for this weblog 🙂
On this weblog, we are going to find out about saving Customized CMS block on the admin finish. We already know the right way to create the identical utilizing the setup/patch (https://webkul.com/weblog/create-cms-block-installer-magento-2/). On this method, we do need to run Magento instructions to register the identical, to beat with identical we are able to do the identical job through the use of the CMS mannequin i.e. “MagentoCmsModelBlockFactory”.
On this mannequin, we are able to use the perform save() so as to add the cms block programmatically.
Let’s examine the code on the right way to do the identical:
First, we are going to create a module by which we are going to create a controller to avoid wasting the CMS block for that we require knowledge to submit for that we are going to create a type through the use of the .phtml file.
<div> <type methodology="publish" motion="<?= $block->escapeUrl($block->getUrl('cmsblocksave/block/save')); ?>" enctype="multipart/form-data" data-mage-init='{"validation":{}}' id="wk-mpzcv-form"> <fieldset class="fieldset"> <?= $block->escapeHtml($block->getFormKeyBlockHtml()); ?> <legend class="legend"> <span><?= $block->escapeHtml(__("CMS Block")); ?></span> <button class="button wk-mp-btn" kind="submit"> <span><span><?= $block->escapeHtml(__("Save CMS Block")); ?></span></span> </button> </legend> <div class="area required"> <label for="mpzcv-region" class="label"> <span><?= $block->escapeHtml(__("Title")); ?></span> </label> <div class="management"> <enter kind="textual content" class="input-text required-entry validate-no-html-tags" title="<?= $block->escapeHtml(__('Title')); ?>" identify="title" id="mpzcv-region" aria-required="true"> </div> </div> <div class="area required"> <label for="mpzcv-region" class="label"> <span><?= $block->escapeHtml(__("identifier")); ?></span> </label> <div class="management"> <enter kind="textual content" class="input-text required-entry validate-no-html-tags" title="<?= $block->escapeHtml(__('identifier')); ?>" identify="identifier" id="mpzcv-region" aria-required="true"> </div> </div> <div class="area required"> <label for="mpzcv-zipcodes" class="label"> <span><?= $block->escapeHtml(__("is_active")); ?></span> </label> <div class="management"> <choose identify="is_active" class="required-entry"> <choice worth="1"> <?= $block->escapeHtml(__("Allow")); ?> </choice> <choice worth="0"> <?= $block->escapeHtml(__('Disable')); ?> </choice> </choose> </div> </div> <div class="area required"> <label for="mpzcv-zipcodes" class="label"> <span><?= $block->escapeHtml(__("is_active")); ?></span> </label> <div class="management"> <choose a number of = "a number of" identify="is_active" class="required-entry"> <choice worth="0">All Retailer Views </choice> <choice worth="1">Default Retailer View </choice> </choose> </div> </div> </fieldset> </type> </div>
Now we are going to create a controller to hit the phtml file. like Controller/Block/Add.php
<?php namespace WebkulCmsBlockSaveControllerBlock; use MagentoFrameworkAppActionAction; use MagentoFrameworkAppActionContext; use MagentoFrameworkViewResultPageFactory; use MagentoFrameworkAppRequestInterface; class Add extends Motion { /** * @var MagentoCustomerModelSession */ protected $_customerSession; /** * @var PageFactory */ protected $resultPageFactory; /** * @var MagentoCustomerModelUrl */ protected $url; /** * @param Context $context * @param PageFactory $resultPageFactory * @param MagentoCustomerModelSession $customerSession * @param MagentoCustomerModelUrl $url */ public perform __construct( Context $context, PageFactory $resultPageFactory, MagentoCustomerModelSession $customerSession, MagentoCustomerModelUrl $url, ) { $this->_customerSession = $customerSession; $this->resultPageFactory = $resultPageFactory; $this->url = $url; mother or father::__construct($context); } /** * Add Retailer Web page * * @return MagentoFrameworkViewResultPage */ public perform execute() { /** @var MagentoFrameworkViewResultPage $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->set(__('Add Cms Block')); return $resultPage; } }
to hit the controller now we have to create a format file: cmsblocksave_block_add.xml
<?xml model="1.0"?> <web page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Format/and so on/page_configuration.xsd"> <physique> <referenceContainer identify="content material"> <block class="WebkulCmsBlockSaveBlockCmsAdd" identify="cmsblocksave_block_add" template="Webkul_CmsBlockSave::add.phtml" cacheable="false"/> </referenceContainer> </physique> </web page>
Type will likely be like talked about beneath:

By this code we will present the shape to avoid wasting the customized CMS block on the admin finish.
Now we are going to examine the code to avoid wasting the cms block talked about beneath
<?php namespace WebkulCmsBlockSaveControllerBlock; use MagentoFrameworkAppActionAction; use MagentoFrameworkAppActionContext; use MagentoFrameworkViewResultPageFactory; use MagentoCmsModelBlockFactory; /** * Save CMS block motion. */ class Save extends Motion { /** * @var DataPersistorInterface */ protected $resultPageFactory; /** * @var BlockFactory */ personal $blockFactory; /** * @var BlockRepositoryInterface */ personal $coreRegistry; personal $jsonFactory; /** * @param Context $context * @param Registry $coreRegistry * @param DataPersistorInterface $dataPersistor * @param BlockFactory|null $blockFactory * @param BlockRepositoryInterface|null $blockRepository */ public perform __construct( Context $context, PageFactory $resultPageFactory, MagentoFrameworkRegistry $coreRegistry, MagentoFrameworkControllerResultJsonFactory $jsonFactory, BlockFactory $blockFactory ) { $this->resultPageFactory = $resultPageFactory; $this->coreRegistry = $coreRegistry; $this->jsonFactory = $jsonFactory; $this->blockFactory = $blockFactory; mother or father::__construct($context); } /** * Save motion * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return MagentoFrameworkControllerResultInterface */ public perform execute() { $testBlock = $this->getRequest()->getPostValue(); $this->blockFactory->create()->setData($testBlock)->save(); $returnData = ['status' => true, 'data' => $testBlock]; return $this->resultRedirectFactory->create()->setPath( 'cmsblocksave/block/add', ['_secure' => $this->getRequest()->isSecure()] ); return $this->jsonFactory->create()->setData($returnData); } }
Output will likely be.

That’s all for this weblog 🙂