Skip to main content

Posts

Showing posts from April, 2023

How to add CMS block Programmatically using Patch?

Whenever we deal with content in Magento, we have requirements where we want to provide the power of changing content to the admin but at the same time, we want to display that block's content somewhere according to the developer's code. OR if you want to create multiple blocks with Magento setup, then you can use the below approach to create them. Create a Patch file in your module at MageInsight/ModuleName/Setup/Patch/Data/AddCmsBlock.php <?php namespace MageInsight\ ModuleName\Setup\Patch\Data; use Magento\Cms\Model\BlockFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Magento\Store\Model\Store; class AddCmsBlock implements DataPatchInterface, PatchRevertableInterface { const CMS_BLOCK_IDENTIFIER = 'block_name'; /** * @var \Magento\Framework\Setup\ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var BlockFactory */