I have updated content for the existing cms page by using data patches in Magento 2.3.3, but the same approach not working for CMS block.
<?php
namespace Test\CmsExport\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Cms\Model\BlockFactory;
class UpdateTest2Block implements DataPatchInterface, PatchVersionInterface {
private $blockFactory;
private $moduleDataSetup;
public function __construct(
PageFactory $blockFactory,
ModuleDataSetupInterface $moduleDataSetup
) {
$this->blockFactory = $blockFactory;
$this->moduleDataSetup = $moduleDataSetup;
}
public function apply () {
$this->moduleDataSetup->startSetup();
$test2BlockContent = '<p>Test 2 block updated using data patches</p>';
$test2Block = $this->createBlock()->load(
'test2',
'identifier'
);
$test2BlockId = $test2Block->getId();
if ($test2BlockId) {
$test2Block->setContent($test2BlockContent);
$test2Block->save();
}
$this->moduleDataSetup->endSetup();
}
public static function getDependencies() {
return [];
}
public static function getVersion() {
return '1.0.1';
}
public function getAliases() {
return [];
}
private function createBlock() {
return $this->blockFactory->create();
}
}