blocks.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. use Magento\Cms\Api\BlockRepositoryInterface;
  8. use Magento\Cms\Api\Data\BlockInterface;
  9. use Magento\Cms\Api\Data\BlockInterfaceFactory;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. /** @var BlockRepositoryInterface $blockRepository */
  13. $blockRepository = Bootstrap::getObjectManager()->get(BlockRepositoryInterface::class);
  14. /** @var BlockInterfaceFactory $blockFactory */
  15. $blockFactory = Bootstrap::getObjectManager()->get(BlockInterfaceFactory::class);
  16. $storeId = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)->getStore()->getId();
  17. /** @var BlockInterface $block */
  18. $block = $blockFactory->create([
  19. 'data' => [
  20. BlockInterface::IDENTIFIER => 'enabled_block',
  21. BlockInterface::TITLE => 'Enabled CMS Block Title',
  22. BlockInterface::CONTENT => '
  23. <h1>Enabled Block</h1>
  24. <a href="{{store url=""}}">store url</a>
  25. <p>Config value: "{{config path="web/unsecure/base_url"}}".</p>
  26. <p>Custom variable: "{{customvar code="variable_code"}}".</p>
  27. ',
  28. BlockInterface::IS_ACTIVE => 1,
  29. 'store_id' => [$storeId],
  30. ]
  31. ]);
  32. $blockRepository->save($block);
  33. /** @var BlockInterface $block */
  34. $block = $blockFactory->create([
  35. 'data' => [
  36. BlockInterface::IDENTIFIER => 'disabled_block',
  37. BlockInterface::TITLE => 'Disabled CMS Block Title',
  38. BlockInterface::CONTENT => '
  39. <h1>Disabled Block</h1>
  40. <a href="{{store url=""}}">store url</a>
  41. <p>Config value: "{{config path="web/unsecure/base_url"}}".</p>
  42. <p>Custom variable: "{{customvar code="variable_code"}}".</p>
  43. ',
  44. BlockInterface::IS_ACTIVE => 0,
  45. 'store_id' => [$storeId],
  46. ]
  47. ]);
  48. $blockRepository->save($block);