Created
October 6, 2021 09:17
-
-
Save magevision/e5f3e82f67ceb2a0073bc29d8dafb972 to your computer and use it in GitHub Desktop.
CreateCmsPageDataPatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MageVision\Blog70\Setup\Patch\Data; | |
use Magento\Cms\Model\PageFactory; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
use Magento\Framework\Setup\Patch\PatchRevertableInterface; | |
use Magento\Store\Model\Store; | |
class CreateCmsPage implements DataPatchInterface, PatchRevertableInterface | |
{ | |
const CMS_PAGE_IDENTIFIER = 'sample-cms-page'; | |
/** | |
* @var ModuleDataSetupInterface | |
*/ | |
private $moduleDataSetup; | |
/** | |
* @var PageFactory | |
*/ | |
private $pageFactory; | |
/** | |
* @param ModuleDataSetupInterface $moduleDataSetup | |
* @param PageFactory $pageFactory | |
*/ | |
public function __construct( | |
ModuleDataSetupInterface $moduleDataSetup, | |
PageFactory $pageFactory | |
) { | |
$this->moduleDataSetup = $moduleDataSetup; | |
$this->pageFactory = $pageFactory; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function apply() | |
{ | |
$this->moduleDataSetup->startSetup(); | |
$this->pageFactory->create() | |
->setTitle('Sample CMS Page') | |
->setIdentifier(self::CMS_PAGE_IDENTIFIER) | |
->setIsActive(true) | |
->setPageLayout('1column') | |
->setContent('<div>Sample CMS Page Content</div>') | |
->setStores([Store::DEFAULT_STORE_ID]) | |
->save(); | |
$this->moduleDataSetup->endSetup(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function revert() | |
{ | |
$sampleCmsPage = $this->pageFactory | |
->create() | |
->load(self::CMS_PAGE_IDENTIFIER, 'identifier'); | |
if ($sampleCmsPage->getId()) { | |
$sampleCmsPage->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