GetBlockByIdentifier.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model;
  7. use Magento\Cms\Api\GetBlockByIdentifierInterface;
  8. use Magento\Cms\Api\Data\BlockInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * Class GetBlockByIdentifier
  12. */
  13. class GetBlockByIdentifier implements GetBlockByIdentifierInterface
  14. {
  15. /**
  16. * @var \Magento\Cms\Model\BlockFactory
  17. */
  18. private $blockFactory;
  19. /**
  20. * @var ResourceModel\Block
  21. */
  22. private $blockResource;
  23. /**
  24. * @param BlockFactory $blockFactory
  25. * @param ResourceModel\Block $blockResource
  26. */
  27. public function __construct(
  28. \Magento\Cms\Model\BlockFactory $blockFactory,
  29. \Magento\Cms\Model\ResourceModel\Block $blockResource
  30. ) {
  31. $this->blockFactory = $blockFactory;
  32. $this->blockResource = $blockResource;
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function execute(string $identifier, int $storeId) : BlockInterface
  38. {
  39. $block = $this->blockFactory->create();
  40. $block->setStoreId($storeId);
  41. $this->blockResource->load($block, $identifier, BlockInterface::IDENTIFIER);
  42. if (!$block->getId()) {
  43. throw new NoSuchEntityException(__('The CMS block with the "%1" ID doesn\'t exist.', $identifier));
  44. }
  45. return $block;
  46. }
  47. }