Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. class InstallData implements InstallDataInterface
  2. {
  3. /**
  4. * @var BlockRepositoryInterface
  5. */
  6. private $blockRepository;
  7. /**
  8. * @var BlockInterfaceFactory
  9. */
  10. private $blockInterfaceFactory;
  11.  
  12. private $pageFactory;
  13.  
  14. public function __construct(
  15. PageFactory $pageFactory,
  16. BlockRepositoryInterface $blockRepository,
  17. BlockInterfaceFactory $blockInterfaceFactory
  18. ) {
  19. $this->pageFactory = $pageFactory;
  20. $this->blockRepository = $blockRepository;
  21. $this->blockInterfaceFactory = $blockInterfaceFactory;
  22. }
  23.  
  24.  
  25. /**
  26. * Installs data for a module
  27. *
  28. * @param ModuleDataSetupInterface $setup
  29. * @param ModuleContextInterface $context
  30. * @return void
  31. */
  32. public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  33. {
  34. $this->createCopyRightBlock();
  35. }
  36. public function createCopyRightBlock()
  37. {
  38. $cmsBlock = $this->blockInterfaceFactory->create();
  39.  
  40. $content = "<div class='copy-right'><p>Copyright © 2017 Test Title</p></div>";
  41. $identifier = "copy-right";
  42. $title = "Copy Right Block";
  43.  
  44. // We can then use the exposed methods from
  45. // MagentoCmsApiDataBlockInterface to set data to our CMS block
  46. $cmsBlock->setData('stores', [0])
  47. ->setIdentifier($identifier)
  48. ->setIsActive(1)
  49. ->setTitle($title)
  50. ->setContent($content);
  51.  
  52. // And use the MagentoCmsApiBlockRepositoryInterface::save
  53. // to actually save our CMS block
  54. $this->blockRepository->save($cmsBlock);
  55. } }
  56.  
  57. $cmsBlock = $this->blockInterfaceFactory->create();
  58.  
  59. $copyrightBlock = $cmsBlock->load('copy-right','identifier');
  60. //And then check
  61.  
  62. if (!$copyrightBlock->getId()) {
  63. ......
  64. }
  65.  
  66. $footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');
  67.  
  68. try {
  69. $this->blockRepository->save($cmsBlock);
  70. } catch (Exception $e) {
  71. // Do nothing, block likely already exists
  72. }
  73.  
  74. <?php
  75. namespace VendorNamespaceSetup;
  76.  
  77. use MagentoCmsModelBlock as CmsBlock;
  78. use MagentoCmsModelBlockRepository;
  79. use MagentoCmsModelBlockFactory;
  80. use MagentoFrameworkExceptionNoSuchEntityException;
  81. use MagentoFrameworkSetupModuleContextInterface;
  82. use MagentoFrameworkSetupModuleDataSetupInterface;
  83. use MagentoFrameworkSetupUpgradeDataInterface;
  84. use MagentoStoreModelStore;
  85.  
  86. /**
  87. * Class UpgradeData
  88. */
  89. class UpgradeData implements UpgradeDataInterface
  90. {
  91. /**
  92. * @var BlockFactory
  93. */
  94. private $blockFactory;
  95.  
  96. /**
  97. * @var BlockRepository
  98. */
  99. private $blockRepository;
  100.  
  101. /**
  102. * UpgradeData constructor.
  103. *
  104. * @param BlockFactory $blockFactory
  105. * @param BlockRepository $blockRepository
  106. */
  107. public function __construct(
  108. BlockFactory $blockFactory,
  109. BlockRepository $blockRepository
  110. ) {
  111. $this->blockFactory = $blockFactory;
  112. $this->blockRepository = $blockRepository;
  113. }
  114.  
  115. /**
  116. * Upgrades data for a module
  117. *
  118. * @param ModuleDataSetupInterface $setup
  119. * @param ModuleContextInterface $context
  120. *
  121. * @return void
  122. * @throws Exception
  123. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  124. */
  125. public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  126. {
  127. if (version_compare($context->getVersion(), '1.0.1', '<')) {
  128. $cmsBlocksData = [
  129. [
  130. 'title' => 'Title',
  131. 'identifier' => 'cms_block_identifier',
  132. 'content' => '<p>Some content</p>',
  133. 'is_active' => CmsBlock::STATUS_ENABLED,
  134. 'stores' => Store::DEFAULT_STORE_ID
  135. ],
  136. [
  137. 'title' => 'Title 2',
  138. 'identifier' => 'cms_block_identifier_second',
  139. 'content' => '<p>Some content</p>',
  140. 'is_active' => CmsBlock::STATUS_ENABLED,
  141. 'stores' => Store::DEFAULT_STORE_ID
  142. ]
  143. ];
  144.  
  145. foreach ($cmsBlocksData as $cmsBlockData) {
  146. /** @var CmsBlock $block */
  147. try {
  148. $this->blockRepository->getById($cmsBlockData['identifier']);
  149. } catch (NoSuchEntityException $e) {
  150. $block = $this->blockFactory->create();
  151. $block->setData($cmsBlockData);
  152. $this->blockRepository->save($block);
  153. }
  154.  
  155. }
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement