0

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();
    }
  }

1 Answer 1

0

Try this way...

<?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 
 * @package Test\CmsExport\Setup\Patch\Data
 */
class UpdateTest2Block implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var BlockFactory
     */
    private $blockFactory;

    /**
     * AddAccessViolationPageAndAssignB2CCustomers constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param PageFactory $blockFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        BlockFactory $blockFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->blockFactory = $blockFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $newCmsStaticBlock = [
            'title' => 'Terms & Conditions',
            'identifier' => 'terms-conditions',
            'content' => '<div class="cms-terms">CMS Static Block Content goes here</div>',
            'is_active' => 1,
            'stores' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
        ];

        $this->moduleDataSetup->startSetup();

        /** @var \Magento\Cms\Model\Block $block */
        $block = $this->blockFactory->create();
        $block->setData($newCmsStaticBlock)->save();

        $this->moduleDataSetup->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '2.0.0';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}
1
  • Found the issue in that file and fixed it. Thanks Feb 5, 2020 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.