vendor/sulu/article-bundle/EventListener/ContentProxyListener.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\ArticleBundle\EventListener;
  11. use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
  12. use Sulu\Bundle\ArticleBundle\Document\Structure\ContentProxyFactory;
  13. use Sulu\Bundle\ArticleBundle\Elasticsearch\PostConvertToDocumentEvent;
  14. use Sulu\Component\Content\Compat\StructureInterface;
  15. use Sulu\Component\Content\Compat\StructureManagerInterface;
  16. /**
  17.  * This event-listener extends the view-document with the content-proxy.
  18.  */
  19. class ContentProxyListener
  20. {
  21.     /**
  22.      * @var ContentProxyFactory
  23.      */
  24.     private $contentProxyFactory;
  25.     /**
  26.      * @var StructureManagerInterface
  27.      */
  28.     private $structureManager;
  29.     public function __construct(ContentProxyFactory $contentProxyFactoryStructureManagerInterface $structureManager)
  30.     {
  31.         $this->contentProxyFactory $contentProxyFactory;
  32.         $this->structureManager $structureManager;
  33.     }
  34.     /**
  35.      * Add the proxies for content and view to view-documents.
  36.      */
  37.     public function onPostConvertToDocument(PostConvertToDocumentEvent $event): void
  38.     {
  39.         $document $event->getDocument();
  40.         if (!$document instanceof ArticleViewDocumentInterface) {
  41.             return;
  42.         }
  43.         $structure $this->structureManager->getStructure($document->getStructureType(), 'article');
  44.         if (!$structure) {
  45.             throw new \RuntimeException(\sprintf('Could not find article structure from type "%s".'$document->getStructureType()));
  46.         }
  47.         $structure->setUuid($document->getUuid());
  48.         $structure->setLanguageCode($document->getLocale());
  49.         list($content$view) = $this->getProxies($document->getContentData(), $structure);
  50.         $document->setContent($content);
  51.         $document->setView($view);
  52.         foreach ($document->getPages() as $page) {
  53.             $structure->setUuid($page->uuid);
  54.             list($page->content$page->view) = $this->getProxies($page->contentData$structure);
  55.         }
  56.     }
  57.     /**
  58.      * Create content and view proxy for given content-data.
  59.      */
  60.     private function getProxies(string $contentDataStructureInterface $structure): array
  61.     {
  62.         $contentData $contentData ?: '{}';
  63.         $data = \json_decode($contentDatatrue);
  64.         $content $this->contentProxyFactory->createContentProxy($structure$data);
  65.         $view $this->contentProxyFactory->createViewProxy($structure$data);
  66.         return [$content$view];
  67.     }
  68. }