In Magento 2, CMS blocks are lines of HTML code that can be placed in multiple places and repeated usage.
We can also add these CMS blocks inside CMS Pages and other blocks.
In today’s tutorial, we are going to learn how we can create CMS blocks programmatically in Magento 2.
To create a CMS block you require the following values :
Title – The title of the CMS block
Identifier – the unique identifier key for the block
Store Views – defines the scope for the CMS Block
Status – Defines if the block is visible or not
Content – The HTML content that will be displayed while calling the CMS Block
To create a CMS Block programmatically we can use InstallData to achieve it.
I’m assuming you have already created your custom module, If not use this tutorial to quickly create a simple module in Magento 2.
Create the below file and update the content as per requirement :
Example: app/code/Jigar/Tutorials/Setup/InstallData.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php /** * * Tutorial by Jigar Karangiya Blogs, hello@jigarkarangiya.com * Visit jigarkarangiya.com for more magento 2 blogs. */ namespace Jigar\Tutorials\Setup; use Magento\Cms\Model\BlockFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Cms\Api\Data\BlockInterface; class InstallData implements InstallDataInterface { private $blockFactory; public function __construct(BlockFactory $blockFactory) { $this->blockFactory = $blockFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $data = [ BlockInterface::IDENTIFIER => 'Jigar CMS Block Example', BlockInterface::TITLE => 'jigar-cms-block-example', BlockInterface::CONTENT => 'Hello Visitors, This is an example of how to create CMS Block programatically in magento 2', BlockInterface::IS_ACTIVE => 1, 'stores' => [0,1,2], 'sort_order' => 10 ]; $this->blockFactory->create()->setData($data)->save(); } } |
After the deployment, you will see the block is created in your Magento 2 instance.
You can check in the CMS Block grid by navigating to Content > Blocks Grid inside the Magento 2 admin panel.
CMS blocks are stored in the cms_block table and mapped with the cms_block_store table.
You can now use and render the create block using the below snippets.
From CMS Page :
1 |
{{block class="Magento\Cms\Block\Block" block_id="jigar-cms-block-example"}} |
From XML File:
For Example, cms_index_index.xml
Add the block using the identifier below :
1 2 3 4 5 6 7 |
<referenceContainer name="content"> <block class="Magento\Cms\Block\Block" name="{{block class="Magento\Cms\Block\Block" block_id="jigar_cms_block_example"}}"> <arguments> <argument name="block_id" xsi:type="string">{{block class="Magento\Cms\Block\Block" block_id="jigar-cms-block-example"}}</argument> </arguments> </block> </referenceContainer> |
Run the below commands to reflect changes :
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:flush |
That’s it, Now you have learned how to create CMS Blocks programmatically using Magento 2 modules.
It will be very useful when you have to create many CMS Blocks and need to import them in multiple instances, You will not required to manually move each CMS Blocks.
I hope this tutorial helped you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
Do share this tutorial with your team.
Thank you. Happy Coding !!
You may also like :
How to validate email address in Magento 2 programmatically?
Magento 2 : What is Router Id = admin & standard ?
Demonstrate ability to use and customize Categories in Magento 2
People also searched for :
- cms block programatically magento 2
- create cms block programmatically magento 2
- magento 2 update cms block programmatically
- magento 2 get cms block programmatically
- magento cms block
- create block programmatically magento 2
- cms block
- cms block in magento 2
- magento 2 add block to cms page
- magento 2 get cms block by identifier in phtml
- magento dynamic blocks
- magento 2 create block programmatically in phtml
- magento 2 cms block in phtml
- magento 2 blocks
- magento 2 get cms block in phtml
- add cms block in phtml magento 2
- magento 2 block set template
- add cms block in xml magento 2
- cms block programatically magento 2
- create cms block programmatically magento 2
- magento 2 update cms block programmatically
- magento 2 get cms block programmatically
- magento cms block
- create block programmatically magento 2
- cms block
- cms block in magento 2
- magento 2 add block to cms page
- magento 2 get cms block by identifier in phtml
- magento dynamic blocks
- magento 2 create block programmatically in phtml
- magento 2 cms block in phtml
- magento 2 blocks
- magento 2 get cms block in phtml
- add cms block in phtml magento 2
- magento 2 block set template
- add cms block in xml magento 2
- cms block programatically magento 2
- magento cms block
- is magento a cms
- magento 2 blocks
- magento 2 update cms block programmatically
- create block programmatically magento 2
- magento 2 cms block in phtml
- cms block in magento 2
- add cms block in xml magento 2
- magento 2 get cms block by identifier in phtml
- create cms block programmatically magento 2
- how to create cms block in magento 2
- magento dynamic blocks
- magento 2 check if cms block exists