|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Snowdog\CmsApi\Model; |
| 4 | + |
| 5 | +use Magento\Cms\Api\BlockRepositoryInterface; |
| 6 | +use Magento\Cms\Model\Template\FilterProvider; |
| 7 | +use Snowdog\CmsApi\Api\BlockManagerInterface; |
| 8 | + |
| 9 | +class BlockManager implements BlockManagerInterface |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var BlockRepositoryInterface |
| 13 | + */ |
| 14 | + private $blockRepository; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var FilterProvider |
| 18 | + */ |
| 19 | + private $filterProvider; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + BlockRepositoryInterface $blockRepository, |
| 23 | + FilterProvider $filterProvider |
| 24 | + ) { |
| 25 | + $this->blockRepository = $blockRepository; |
| 26 | + $this->filterProvider = $filterProvider; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @inheritdoc |
| 31 | + */ |
| 32 | + public function getById($blockId) |
| 33 | + { |
| 34 | + $block = $this->blockRepository->getById($blockId); |
| 35 | + $content = $this->getBlockContentFiltered($block->getContent()); |
| 36 | + $block->setContent($content); |
| 37 | + |
| 38 | + return $block; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritdoc |
| 43 | + */ |
| 44 | + public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) |
| 45 | + { |
| 46 | + $blockCollection = $this->blockRepository->getList($searchCriteria); |
| 47 | + $blocks = $blockCollection->getItems(); |
| 48 | + $parsedBlocks = []; |
| 49 | + |
| 50 | + foreach ($blocks as $block) { |
| 51 | + $content = $this->getBlockContentFiltered($block->getContent()); |
| 52 | + $parsedBlocks[] = $block->setContent($content); |
| 53 | + } |
| 54 | + |
| 55 | + $blockCollection->setItems($parsedBlocks); |
| 56 | + |
| 57 | + return $blockCollection; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param string $content |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + private function getBlockContentFiltered($content) |
| 65 | + { |
| 66 | + return $this->filterProvider->getBlockFilter() |
| 67 | + ->filter($content); |
| 68 | + } |
| 69 | +} |
0 commit comments