How to create data patch to insert data into the database for dynamic blocks?
2 Answers
Try below code
<?php
namespace Vendor\ModuleName\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterfaceFactory;
class InsertDynamicBlock implements DataPatchInterface, PatchVersionInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var BlockRepositoryInterface
*/
private $blockRepository;
/**
* @var BlockInterfaceFactory
*/
private $blockFactory;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
BlockRepositoryInterface $blockRepository,
BlockInterfaceFactory $blockFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->blockRepository = $blockRepository;
$this->blockFactory = $blockFactory;
}
public function apply()
{
$this->moduleDataSetup->startSetup();
// Insert logic to create and insert data into dynamic blocks
$blockData = [
'title' => 'Dynamic Block Title',
'identifier' => 'dynamic_block_identifier',
'content' => 'Dynamic Block Content',
'is_active' => 1,
'stores' => [0], // Store IDs
];
$block = $this->blockFactory->create();
$block->setData($blockData);
$this->blockRepository->save($block);
$this->moduleDataSetup->endSetup();
}
public static function getDependencies()
{
return [];
}
public static function getVersion()
{
return '1.0.0';
}
public function revert()
{
$this->moduleDataSetup->startSetup();
// Revert logic if needed
$this->moduleDataSetup->endSetup();
}
public function getAliases()
{
return [];
}
}
Let me know if any quries.
Thanks.
-
Asking for dynamic blocks not cms block. Core Module name: module-banner Commented Feb 28 at 5:28
Refer code below
<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Banner\Model\Banner\Validator;
use Magento\Banner\Model\Banner;
use Psr\Log\LoggerInterface;
/**
* Class BannerDynamicBlock
*/
class BannerDynamicBlock implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var Validator
*/
protected $bannerValidator;
/**
* @var Banner
*/
private $banner;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param Validator $bannerValidator
* @param Banner $banner
* @param LoggerInterface $logger
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
Validator $bannerValidator,
Banner $banner,
LoggerInterface $logger
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->bannerValidator = $bannerValidator;
$this->banner = $banner;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->startSetup();
$this->createDynamicBlock();
$this->moduleDataSetup->endSetup();
}
/**
* @return void
*/
public function createDynamicBlock()
{
$data = [
'default_contents' => '',
'use_default_value' => 'false',
'show_use_default_value' => 'false',
'name' => 'Test',
'is_enabled' => '1',
'types' => ['content'],
'customer_sectment_ids' => [],
'banner_sales_rules' => [],
'store_id' => 0,
'content_readonly' => false,
'readonly' => false
];
/** @var Banner $banner */
$banner = $this->banner;
$data = $this->bannerValidator->prepareSaveData($data);
try {
$this->prepareBannerModelData($banner, $data);
$banner->save();
} catch (\Exception $e) {
$this->logger->error('Cannot save the dynamic block, Exeption: ' . $e . '. Please try again.');
}
}
/**
* @param Banner $banner
* @param array $data
* @return void
*/
protected function prepareBannerModelData(\Magento\Banner\Model\Banner $banner, array $data)
{
//$content = $this->dynamicBlockContent();
$data['store_contents'] = [
0 => "Some content"
];
$data['default_contents'] = [
0 => "Some content"
];
$banner->setData($data);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getVersion()
{
return '1.0.0';
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}