<?php
namespace Vendor\Namespace\Setup;
use Magento\Cms\Model\Block as CmsBlock;
use Magento\Cms\Model\BlockRepository;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Store\Model\Store;
/**
* Class UpgradeData
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @var BlockRepository
*/
private $blockRepository;
/**
* UpgradeData constructor.
*
* @param BlockFactory $blockFactory
* @param BlockRepository $blockRepository
*/
public function __construct(
BlockFactory $blockFactory,
BlockRepository $blockRepository
) {
$this->blockFactory = $blockFactory;
$this->blockRepository = $blockRepository;
}
/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @return void
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$cmsBlocksData = [
[
'title' => 'Title',
'identifier' => 'cms_block_identifier',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0Store::DEFAULT_STORE_ID
],
[
'title' => 'Title 2',
'identifier' => 'cms_block_identifier_second',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0Store::DEFAULT_STORE_ID
]
];
foreach ($cmsBlocksData as $cmsBlockData) {
/** @var CmsBlock $block */
try {
$this->blockRepository->getById($cmsBlockData['identifier']);
} catch (NoSuchEntityException $e) {
$block = $this->blockFactory->create();
$block->setData($cmsBlockData);
$this->blockRepository->save($block);
}
}
}
}
}
<?php
namespace Vendor\Namespace\Setup;
use Magento\Cms\Model\Block as CmsBlock;
use Magento\Cms\Model\BlockRepository;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
/**
* Class UpgradeData
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @var BlockRepository
*/
private $blockRepository;
/**
* UpgradeData constructor.
*
* @param BlockFactory $blockFactory
* @param BlockRepository $blockRepository
*/
public function __construct(
BlockFactory $blockFactory,
BlockRepository $blockRepository
) {
$this->blockFactory = $blockFactory;
$this->blockRepository = $blockRepository;
}
/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @return void
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$cmsBlocksData = [
[
'title' => 'Title',
'identifier' => 'cms_block_identifier',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0
],
[
'title' => 'Title 2',
'identifier' => 'cms_block_identifier_second',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0
]
];
foreach ($cmsBlocksData as $cmsBlockData) {
/** @var CmsBlock $block */
try {
$this->blockRepository->getById($cmsBlockData['identifier']);
} catch (NoSuchEntityException $e) {
$block = $this->blockFactory->create();
$block->setData($cmsBlockData);
$this->blockRepository->save($block);
}
}
}
}
}
<?php
namespace Vendor\Namespace\Setup;
use Magento\Cms\Model\Block as CmsBlock;
use Magento\Cms\Model\BlockRepository;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Store\Model\Store;
/**
* Class UpgradeData
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @var BlockRepository
*/
private $blockRepository;
/**
* UpgradeData constructor.
*
* @param BlockFactory $blockFactory
* @param BlockRepository $blockRepository
*/
public function __construct(
BlockFactory $blockFactory,
BlockRepository $blockRepository
) {
$this->blockFactory = $blockFactory;
$this->blockRepository = $blockRepository;
}
/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @return void
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$cmsBlocksData = [
[
'title' => 'Title',
'identifier' => 'cms_block_identifier',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => Store::DEFAULT_STORE_ID
],
[
'title' => 'Title 2',
'identifier' => 'cms_block_identifier_second',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => Store::DEFAULT_STORE_ID
]
];
foreach ($cmsBlocksData as $cmsBlockData) {
/** @var CmsBlock $block */
try {
$this->blockRepository->getById($cmsBlockData['identifier']);
} catch (NoSuchEntityException $e) {
$block = $this->blockFactory->create();
$block->setData($cmsBlockData);
$this->blockRepository->save($block);
}
}
}
}
}
<?php
namespace Vendor\Namespace\Setup;
use Magento\Cms\Model\Block as CmsBlock;
use Magento\Cms\Model\BlockRepository;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
/**
* Class UpgradeData
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @var BlockRepository
*/
private $blockRepository;
/**
* UpgradeData constructor.
*
* @param BlockFactory $blockFactory
* @param BlockRepository $blockRepository
*/
public function __construct(
BlockFactory $blockFactory,
BlockRepository $blockRepository
) {
$this->blockFactory = $blockFactory;
$this->blockRepository = $blockRepository;
}
/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @return void
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$cmsBlocksData = [
[
'title' => 'Title',
'identifier' => 'cms_block_identifier',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0
],
[
'title' => 'Title 2',
'identifier' => 'cms_block_identifier_second',
'content' => '<p>Some content</p>',
'is_active' => CmsBlock::STATUS_ENABLED,
'stores' => 0
]
];
foreach ($cmsBlocksData as $cmsBlockData) {
/** @var CmsBlock $block */
try {
$this->blockRepository->getById($cmsBlockData['identifier']);
} catch (NoSuchEntityException $e) {
$block = $this->blockFactory->create();
$block->setData($cmsBlockData);
$this->blockRepository->save($block);
}
}
}
}
}
default