This article shows how you can add or update CMS Static Block using Install or Upgrade Script of a module in Magento 2.
This code is useful when you need to auto add/edit cms static block while installing or upgrading your custom module.
Add CMS Static Block from InstallData Setup Script
This will add cms static block when you install your custom module for the first time.
Here, I am using my custom module named Chapagain_HelloWorld
.
File: app/code/Chapagain/HelloWorld/Setup/InstallData.php
<?php
namespace Chapagain\HelloWorld\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
/**
* @var \Magento\Cms\Model\BlockFactory
*/
private $_blockFactory;
/**
* InstallData constructor
*
* @param \Magento\Cms\Model\BlockFactory $blockFactory
*/
public function __construct(
\Magento\Cms\Model\BlockFactory $blockFactory
)
{
$this->_blockFactory = $blockFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
* @throws \Exception
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
// Load cms block by identifier
$cmsBlock = $this->_blockFactory->create()->load('test-block', 'identifier');
// Create CMS Block
if (!$cmsBlock->getId()) {
$cmsBlockData = [
'title' => 'Test Block',
'identifier' => 'test-block',
'content' => "<div class='myclass'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>",
'is_active' => 1,
'stores' => [0]
];
$this->_blockFactory->create()->setData($cmsBlockData)->save();
}
}
}
`
Update CMS Static Block from UpgradeData Setup Script
This will update cms static block when you upgrade your custom module. You can also write the add cms page code in the UpgradeData class. In this example, I will be updating a cms page that’s already been created before.
Here, I am using my custom module named Chapagain_HelloWorld
.
In the below code, you can see that I have checked for my module’s version 0.1.1. So, while upgrading the module to version 0.1.1, the below UpgradeData script will get executed.
File: app/code/Chapagain/HelloWorld/Setup/UpgradeData.php
<?php
namespace Chapagain\HelloWorld\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
const YOUR_STORE_ID = 1;
/**
* @var \Magento\Cms\Model\BlockFactory
*/
private $_blockFactory;
/**
* UpgradeData constructor
*
* @param \Magento\Cms\Model\BlockFactory $blockFactory
*/
public function __construct(
\Magento\Cms\Model\BlockFactory $blockFactory
)
{
$this->_blockFactory = $blockFactory;
}
/**
* Upgrade data for the module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
* @throws \Exception
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
// run the code while upgrading module to version 0.1.1
if (version_compare($context->getVersion(), '0.1.1') < 0) {
$cmsBlock = $this->_blockFactory->create()->setStoreId(self::YOUR_STORE_ID)->load('test-block', 'identifier');
$cmsBlockData = [
'title' => 'Test Block 2',
'identifier' => 'test-block',
'is_active' => 1,
'stores' => [self::YOUR_STORE_ID],
'content' => "<div class='block'>
<h2>My Test Block 2</h2>
</div>",
];
if (!$cmsBlock->getId()) {
$this->_blockFactory->create()->setData($cmsBlockData)->save();
} else {
$cmsBlock->setContent($cmsBlockData['content'])->save();
}
}
$setup->endSetup();
}
}
`
Hope this helps. Thanks.