Last active
August 10, 2020 12:59
-
-
Save 0-Sony/4c3a23816c6829c86d3d112eb25a19ac to your computer and use it in GitHub Desktop.
Magento 2 : Create Cms Block Programmatically
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 Namespace\Cms\Setup; | |
use Magento\Cms\Api\BlockRepositoryInterface; | |
use Magento\Cms\Api\Data\BlockInterface; | |
use Magento\Cms\Api\Data\BlockInterfaceFactory; | |
use Magento\Framework\App\State; | |
use Magento\Framework\App\Area; | |
use Magento\Framework\Setup\InstallDataInterface; | |
use Magento\Framework\Setup\ModuleContextInterface; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
use Magento\Store\Model\Store; | |
class InstallData implements InstallDataInterface | |
{ | |
/** @var BlockRepositoryInterface */ | |
private $blockRepository; | |
/** @var BlockFactory */ | |
private $blockInterfaceFactory; | |
/** @var State */ | |
private $state; | |
/** | |
* InstallData constructor. | |
* @param BlockRepositoryInterface $blockRepository | |
* @param BlockInterfaceFactory $blockInterfaceFactory | |
*/ | |
public function __construct( | |
BlockRepositoryInterface $blockRepository, | |
BlockInterfaceFactory $blockInterfaceFactory, | |
State $state | |
) { | |
$this->blockRepository = $blockRepository; | |
$this->blockInterfaceFactory = $blockInterfaceFactory; | |
$this->state = $state; | |
} | |
/** | |
* Installs data for a module | |
* | |
* @param ModuleDataSetupInterface $setup | |
* @param ModuleContextInterface $context | |
* @return void | |
*/ | |
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | |
{ | |
/** set Area code to prevent the Exception during setup:upgrade */ | |
$this->state->setAreaCode(Area::AREA_ADMINHTML); | |
/** @var BlockInterface $cmsBlock */ | |
$cmsBlock = $this->blockInterfaceFactory->create(); | |
$content = <<<HTML | |
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse mauris tortor, | |
rhoncus at diam ac, aliquet egestas elit. Integer varius, ipsum et imperdiet scelerisque, | |
lorem magna feugiat arcu, sed rhoncus velit magna a velit.</div> | |
HTML; | |
$cmsBlock->setIdentifier('my_custom_identifier'); | |
$cmsBlock->setTitle('my_custom_title'); | |
$cmsBlock->setContent($content); | |
$cmsBlock->setData('stores', [Store::DEFAULT_STORE_ID]); // DEFAULT_STORE_ID = 0 | |
$this->blockRepository->save($cmsBlock); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment