vendor/sulu/article-bundle/Content/ArticleSelectionContentType.php line 57

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\Content;
  11. use ONGR\ElasticsearchBundle\Service\Manager;
  12. use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
  13. use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
  14. use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
  15. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  16. use Sulu\Component\Content\Compat\PropertyInterface;
  17. use Sulu\Component\Content\PreResolvableContentTypeInterface;
  18. use Sulu\Component\Content\SimpleContentType;
  19. /**
  20.  * Provides article_selection content-type.
  21.  */
  22. class ArticleSelectionContentType extends SimpleContentType implements PreResolvableContentTypeInterface
  23. {
  24.     use ArticleViewDocumentIdTrait;
  25.     /**
  26.      * @var Manager
  27.      */
  28.     private $searchManager;
  29.     /**
  30.      * @var ReferenceStoreInterface
  31.      */
  32.     private $referenceStore;
  33.     /**
  34.      * @var string
  35.      */
  36.     private $articleDocumentClass;
  37.     public function __construct(
  38.         Manager $searchManager,
  39.         ReferenceStoreInterface $referenceStore,
  40.         string $articleDocumentClass
  41.     ) {
  42.         parent::__construct('Article', []);
  43.         $this->searchManager $searchManager;
  44.         $this->referenceStore $referenceStore;
  45.         $this->articleDocumentClass $articleDocumentClass;
  46.     }
  47.     public function getContentData(PropertyInterface $property)
  48.     {
  49.         $value $property->getValue();
  50.         if (null === $value || !\is_array($value) || === \count($value)) {
  51.             return [];
  52.         }
  53.         $locale $property->getStructure()->getLanguageCode();
  54.         $repository $this->searchManager->getRepository($this->articleDocumentClass);
  55.         $search $repository->createSearch();
  56.         $search->addQuery(new IdsQuery($this->getViewDocumentIds($value$locale)));
  57.         $search->setSize(\count($value));
  58.         $result = [];
  59.         /** @var ArticleViewDocumentInterface $articleDocument */
  60.         foreach ($repository->findDocuments($search) as $articleDocument) {
  61.             $result[\array_search($articleDocument->getUuid(), $valuefalse)] = $articleDocument;
  62.         }
  63.         \ksort($result);
  64.         return \array_values($result);
  65.     }
  66.     public function preResolve(PropertyInterface $property)
  67.     {
  68.         $uuids $property->getValue();
  69.         if (!\is_array($uuids)) {
  70.             return;
  71.         }
  72.         foreach ($uuids as $uuid) {
  73.             $this->referenceStore->add($uuid);
  74.         }
  75.     }
  76. }