Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InstallData implements InstallDataInterface
- {
- /**
- * @var BlockRepositoryInterface
- */
- private $blockRepository;
- /**
- * @var BlockInterfaceFactory
- */
- private $blockInterfaceFactory;
- private $pageFactory;
- public function __construct(
- PageFactory $pageFactory,
- BlockRepositoryInterface $blockRepository,
- BlockInterfaceFactory $blockInterfaceFactory
- ) {
- $this->pageFactory = $pageFactory;
- $this->blockRepository = $blockRepository;
- $this->blockInterfaceFactory = $blockInterfaceFactory;
- }
- /**
- * Installs data for a module
- *
- * @param ModuleDataSetupInterface $setup
- * @param ModuleContextInterface $context
- * @return void
- */
- public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
- {
- $this->createCopyRightBlock();
- }
- public function createCopyRightBlock()
- {
- $cmsBlock = $this->blockInterfaceFactory->create();
- $content = "<div class='copy-right'><p>Copyright © 2017 Test Title</p></div>";
- $identifier = "copy-right";
- $title = "Copy Right Block";
- // We can then use the exposed methods from
- // MagentoCmsApiDataBlockInterface to set data to our CMS block
- $cmsBlock->setData('stores', [0])
- ->setIdentifier($identifier)
- ->setIsActive(1)
- ->setTitle($title)
- ->setContent($content);
- // And use the MagentoCmsApiBlockRepositoryInterface::save
- // to actually save our CMS block
- $this->blockRepository->save($cmsBlock);
- } }
- $cmsBlock = $this->blockInterfaceFactory->create();
- $copyrightBlock = $cmsBlock->load('copy-right','identifier');
- //And then check
- if (!$copyrightBlock->getId()) {
- ......
- }
- $footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');
- try {
- $this->blockRepository->save($cmsBlock);
- } catch (Exception $e) {
- // Do nothing, block likely already exists
- }
- <?php
- namespace VendorNamespaceSetup;
- use MagentoCmsModelBlock as CmsBlock;
- use MagentoCmsModelBlockRepository;
- use MagentoCmsModelBlockFactory;
- use MagentoFrameworkExceptionNoSuchEntityException;
- use MagentoFrameworkSetupModuleContextInterface;
- use MagentoFrameworkSetupModuleDataSetupInterface;
- use MagentoFrameworkSetupUpgradeDataInterface;
- use MagentoStoreModelStore;
- /**
- * 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);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement