Magento 2: How to Create Cms Block Programmatically Using DataPatch

Introduction In the [How to Create Cms Page Programmatically Using DataPatch] chapter I explained why we should use DataPatch in the Magento 2. To create CMS-Block we laso will use DataPatch. It will save our time in the future. Solution <?php namespace Vendor\ModuleName\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Magento\Cms\Model\BlockFactory; class CreateCmsBlock implements DataPatchInterface, PatchRevertableInterface { /** * @var BlockFactory */ protected BlockFactory $blockFactory; /** * @param BlockFactory $blockFactory */ public function __construct( BlockFactory $blockFactory ) { $this->blockFactory = $blockFactory; } /** * {@inheritdoc} */ public function apply() { $blockData = [ 'title' => 'Test CMS Block', 'identifier' => 'test_cms_block_identifier', 'content' => 'Yau!...

June 13, 2023 · 2 min

Magento 2: How to Call Cms Block in PHTML Template Correctly

Introduction I’m sure you know how to do it. Just paste the following code into the necessary *.phtml template and that’s it: <?php echo $this->getLayout() ->createBlock(\Magento\Cms\Block\Block::class) ->setIdentifier('block_identifier') ->toHtml(); ?> But! Did you know that the \Magento\Cms\Block\Block is depricated since magento 2.4.2 ? So, the solution above is work for Magento 2.4.1 and less. And for Magento 2.4.2 and higher the solution below. Solution If you updated to the newest Magento version (2....

June 3, 2023 · 1 min