Last active
May 11, 2023 19:26
-
-
Save LucasCalazans/058b2cbfd0d247b2a4ac0bd0b53558b7 to your computer and use it in GitHub Desktop.
Data Patch examples
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 | |
namespace Vendor\Module\Setup\Patch\Data; | |
use Magento\Cms\Model\BlockFactory; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
/** | |
* Class UpdateCMSBlock | |
* @package Vendor\Module\Setup\Patch\Data | |
* @codeCoverageIgnore | |
*/ | |
// @codingStandardsIgnoreLine | |
class UpdateCMSBlock implements DataPatchInterface | |
{ | |
/** | |
* @var BlockFactory | |
*/ | |
private $blockFactory; | |
const BLOCK_IDENTIFIER = 'BLOCK_ID_HERE'; | |
/** | |
* UpdateCMSBlock constructor. | |
* @param BlockFactory $blockFactory | |
*/ | |
public function __construct( | |
BlockFactory $blockFactory | |
) { | |
$this->blockFactory = $blockFactory; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function apply() | |
{ | |
$content = <<<HTML | |
<span>Block Content</span> | |
HTML; | |
$blockInstance = $this->blockFactory | |
->create() | |
->load(self::BLOCK_IDENTIFIER, 'identifier'); | |
if (!$blockInstance->getId()) { | |
return; | |
} | |
$blockInstance->setContent($content)->save(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getAliases() | |
{ | |
return []; | |
} | |
} |
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 | |
namespace Vendor\Module\Setup\Patch\Data; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
use Magento\Cms\Model\PageFactory; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class UpdateCMSPage implements DataPatchInterface | |
{ | |
/** | |
* @var PageFactory | |
*/ | |
protected $pageFactory; | |
const PAGE_IDENTIFIER = 'PAGE_ID_HERE'; | |
/** | |
* Construct | |
* | |
* @param PageFactory $pageFactory | |
*/ | |
public function __construct( | |
PageFactory $pageFactory | |
) { | |
$this->pageFactory = $pageFactory; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function apply() | |
{ | |
$content = <<<HTML | |
<span>Page Content</span> | |
HTML; | |
$pageInstance = $this->pageFactory->create() | |
->load(self::PAGE_IDENTIFIER, 'identifier');; | |
if (!$pageInstance->getId()) { | |
$pageInstance | |
->setTitle('Title') | |
->setStoreId(0) | |
->setPageLayout('cms-full-width') | |
->setIdentifier(self::PAGE_IDENTIFIER); | |
} | |
$pageInstance->setContent($content)->save(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getAliases() | |
{ | |
return []; | |
} | |
} |
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 | |
namespace Vendor\Module\Setup\Patch\Data; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
use Magento\Config\Model\ResourceModel\Config; | |
/** | |
* Class UpdateCoreConfigData | |
* @package Vendor\Module\Setup\Patch\Data | |
* @codeCoverageIgnore | |
*/ | |
// @codingStandardsIgnoreLine | |
class UpdateCoreConfigData implements DataPatchInterface | |
{ | |
/** @var Config */ | |
private $config; | |
/** | |
* @param Config $config | |
*/ | |
public function __construct( | |
Config $config | |
) | |
{ | |
$this->config = $config; | |
} | |
public function apply() | |
{ | |
$this->config->saveConfig( | |
'INSERT_PATH_HERE', | |
'VALUE' | |
); | |
} | |
public function getAliases() | |
{ | |
return []; | |
} | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
} |
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 | |
namespace Vendor\Module\Setup\Patch\Data; | |
use Psr\Log\LoggerInterface; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
use Magento\Config\Model\ResourceModel\Config; | |
use Magento\Store\Api\WebsiteRepositoryInterface; | |
use Magento\Store\Api\StoreRepositoryInterface; | |
/** | |
* Class UpdateCoreConfigData | |
* @package Vendor\Module\Setup\Patch\Data | |
*/ | |
class UpdateCoreConfigData implements DataPatchInterface | |
{ | |
/** | |
* @var Config | |
*/ | |
private $config; | |
/** | |
* @var WebsiteRepositoryInterface | |
*/ | |
public $websiteRepository; | |
/** | |
* @var LoggerInterface | |
*/ | |
public $logger; | |
/** | |
* @var StoreRepositoryInterface | |
*/ | |
public $storeRepository; | |
/** | |
* @param Config $config | |
* @param LoggerInterface $logger | |
* @param WebsiteRepositoryInterface $websiteRepository | |
* @param StoreRepositoryInterface $storeRepository | |
*/ | |
public function __construct( | |
Config $config, | |
LoggerInterface $logger, | |
WebsiteRepositoryInterface $websiteRepository, | |
StoreRepositoryInterface $storeRepository, | |
) | |
{ | |
$this->config = $config; | |
$this->logger = $logger; | |
$this->websiteRepository = $websiteRepository; | |
$this->storeRepository = $storeRepository; | |
} | |
public function apply() | |
{ | |
$websiteId = $this->getWebsiteIdByWebsiteCode('base'); | |
if (!$websiteId) { | |
return; | |
} | |
$this->config->saveConfig( | |
'INSERT_PATH_HERE', | |
'VALUE' | |
'websites', | |
$websiteId | |
); | |
} | |
/** | |
* @param string $code | |
* @return int|null | |
*/ | |
public function getWebsiteIdByWebsiteCode(string $code): ?int | |
{ | |
$websiteId = null; | |
try { | |
$website = $this->websiteRepository->get($code); | |
$websiteId = $website->getId(); | |
} catch (\Exception $exception) { | |
$this->logger->error(__('[Patch::UpdateCoreConfigData] Failed to get website id. Original error: %1', $exception->getMessage())); | |
} | |
return $websiteId; | |
} | |
/** | |
* @param string $code | |
* @return int|null | |
*/ | |
public function getStoreIdByStoreCode(string $code): ?int | |
{ | |
$storeId = null; | |
try { | |
$store = $this->storeRepository->get($code); | |
$storeId = $store->getId(); | |
} catch (\Exception $exception) { | |
$this->logger->error(__('[Patch::UpdateCoreConfigData] Failed to get store id. Original error: %1', $exception->getMessage())); | |
} | |
return $storeId; | |
} | |
public function getAliases() | |
{ | |
return []; | |
} | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment