Skip to content

Instantly share code, notes, and snippets.

@magevision
Created October 6, 2021 12:23
Show Gist options
  • Save magevision/4a07097a59b92e33c1a709a776da8d75 to your computer and use it in GitHub Desktop.
Save magevision/4a07097a59b92e33c1a709a776da8d75 to your computer and use it in GitHub Desktop.
CreateCmsBlock
<?php
declare(strict_types=1);
namespace MageVision\Blog71\Setup\Patch\Data;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Store\Model\Store;
class CreateCmsBlock implements DataPatchInterface, PatchRevertableInterface
{
const CMS_BLOCK_IDENTIFIER = 'sample-cms-block';
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param BlockFactory $blockFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
BlockFactory $blockFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->blockFactory = $blockFactory;
}
/**
* @inheritDoc
*/
public function apply()
{
$this->moduleDataSetup->startSetup();
$this->blockFactory->create()
->setTitle('Sample CMS Block')
->setIdentifier(self::CMS_BLOCK_IDENTIFIER)
->setIsActive(true)
->setContent('<div>Sample CMS Block Content</div>')
->setStores([Store::DEFAULT_STORE_ID])
->save();
$this->moduleDataSetup->endSetup();
}
/**
* {@inheritdoc}
*/
public function revert()
{
$sampleCmsBlock = $this->blockFactory
->create()
->load(self::CMS_BLOCK_IDENTIFIER, 'identifier');
if ($sampleCmsBlock->getId()) {
$sampleCmsBlock->delete();
}
}
/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment