Block.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;
  8. use Magento\Cms\Api\BlockRepositoryInterface;
  9. use Magento\Cms\Api\Data\BlockInterface;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Widget\Model\Template\FilterEmulate;
  12. /**
  13. * Cms block data provider
  14. */
  15. class Block
  16. {
  17. /**
  18. * @var BlockRepositoryInterface
  19. */
  20. private $blockRepository;
  21. /**
  22. * @var FilterEmulate
  23. */
  24. private $widgetFilter;
  25. /**
  26. * @param BlockRepositoryInterface $blockRepository
  27. * @param FilterEmulate $widgetFilter
  28. */
  29. public function __construct(
  30. BlockRepositoryInterface $blockRepository,
  31. FilterEmulate $widgetFilter
  32. ) {
  33. $this->blockRepository = $blockRepository;
  34. $this->widgetFilter = $widgetFilter;
  35. }
  36. /**
  37. * Get block data
  38. *
  39. * @param string $blockIdentifier
  40. * @return array
  41. * @throws NoSuchEntityException
  42. */
  43. public function getData(string $blockIdentifier): array
  44. {
  45. $block = $this->blockRepository->getById($blockIdentifier);
  46. if (false === $block->isActive()) {
  47. throw new NoSuchEntityException(
  48. __('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
  49. );
  50. }
  51. $renderedContent = $this->widgetFilter->filter($block->getContent());
  52. $blockData = [
  53. BlockInterface::IDENTIFIER => $block->getIdentifier(),
  54. BlockInterface::TITLE => $block->getTitle(),
  55. BlockInterface::CONTENT => $renderedContent,
  56. ];
  57. return $blockData;
  58. }
  59. }