Saturday, June 28, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

Save a customized CMS block from frontend utilizing the controller in Magento 2

admin by admin
May 10, 2023
in Services & Software
0
Save a customized CMS block from frontend utilizing the controller in Magento 2
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


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:

Selection_900

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.

Selection_901

That’s all for this weblog 🙂

author-thumb


Ajay Singh
3 Badges

10 Might 2023

RelatedPosts

The state of strategic portfolio administration

The state of strategic portfolio administration

June 11, 2025
You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

June 11, 2025
Consumer Information For Magento 2 Market Limit Vendor Product

Consumer Information For Magento 2 Market Limit Vendor Product

June 11, 2025


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:

Selection_900

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.

Selection_901

That’s all for this weblog 🙂

author-thumb


Ajay Singh
3 Badges

10 Might 2023
Previous Post

(For Southeast Asia) Marvel’s Spider-Man 2 prequel comedian introduced for Free Comedian Guide Day – PlayStation.Weblog

Next Post

Apple’s Combined Actuality Headset May Run Closing Reduce Professional and Logic Professional

Next Post
Apple’s Combined Actuality Headset May Run Closing Reduce Professional and Logic Professional

Apple's Combined Actuality Headset May Run Closing Reduce Professional and Logic Professional

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,401)
  • Gaming (9,599)
  • Home entertainment (633)
  • IOS (9,534)
  • Mobile (11,881)
  • Services & Software (4,006)
  • Tech (5,315)
  • Uncategorized (4)

Recent Posts

  • WWDC 2025 Rumor Report Card: Which Leaks Had been Proper or Unsuitable?
  • The state of strategic portfolio administration
  • 51 of the Greatest TV Exhibits on Netflix That Will Maintain You Entertained
  • ‘We’re previous the occasion horizon’: Sam Altman thinks superintelligence is inside our grasp and makes 3 daring predictions for the way forward for AI and robotics
  • Snap will launch its AR glasses known as Specs subsequent 12 months, and these can be commercially accessible
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept