vendor/sulu/headless-bundle/Content/ContentTypeResolver/PageSelectionResolver.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Sulu.
  5.  *
  6.  * (c) Sulu GmbH
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver;
  12. use Sulu\Bundle\HeadlessBundle\Content\ContentView;
  13. use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface;
  14. use Sulu\Component\Content\Compat\PropertyInterface;
  15. use Sulu\Component\Content\Compat\PropertyParameter;
  16. use Sulu\Component\Content\Mapper\ContentMapperInterface;
  17. use Sulu\Component\Content\Query\ContentQueryBuilderInterface;
  18. class PageSelectionResolver implements ContentTypeResolverInterface
  19. {
  20.     public static function getContentType(): string
  21.     {
  22.         return 'page_selection';
  23.     }
  24.     /**
  25.      * @var StructureResolverInterface
  26.      */
  27.     private $structureResolver;
  28.     /**
  29.      * @var ContentQueryBuilderInterface
  30.      */
  31.     private $contentQueryBuilder;
  32.     /**
  33.      * @var ContentMapperInterface
  34.      */
  35.     private $contentMapper;
  36.     /**
  37.      * @var bool
  38.      */
  39.     private $showDrafts;
  40.     public function __construct(
  41.         StructureResolverInterface $structureResolver,
  42.         ContentQueryBuilderInterface $contentQueryBuilder,
  43.         ContentMapperInterface $contentMapper,
  44.         bool $showDrafts
  45.     ) {
  46.         $this->structureResolver $structureResolver;
  47.         $this->contentQueryBuilder $contentQueryBuilder;
  48.         $this->contentMapper $contentMapper;
  49.         $this->showDrafts $showDrafts;
  50.     }
  51.     public function resolve($dataPropertyInterface $propertystring $locale, array $attributes = []): ContentView
  52.     {
  53.         if (empty($data)) {
  54.             return new ContentView([], ['ids' => []]);
  55.         }
  56.         /** @var PropertyParameter[] $params */
  57.         $params $property->getParams();
  58.         /** @var PropertyParameter[] $propertiesParamValue */
  59.         $propertiesParamValue = isset($params['properties']) ? $params['properties']->getValue() : [];
  60.         $this->contentQueryBuilder->init([
  61.             'ids' => $data,
  62.             'properties' => $propertiesParamValue,
  63.             'published' => !$this->showDrafts,
  64.         ]);
  65.         list($pagesQuery) = $this->contentQueryBuilder->build($property->getStructure()->getWebspaceKey(), [$locale]);
  66.         $pageStructures $this->contentMapper->loadBySql2(
  67.             $pagesQuery,
  68.             $locale,
  69.             $property->getStructure()->getWebspaceKey()
  70.         );
  71.         $propertyMap = [
  72.             'title' => 'title',
  73.             'url' => 'url',
  74.         ];
  75.         foreach ($propertiesParamValue as $propertiesParamEntry) {
  76.             $paramName $propertiesParamEntry->getName();
  77.             $paramValue $propertiesParamEntry->getValue();
  78.             $propertyMap[$paramName] = \is_string($paramValue) ? $paramValue $paramName;
  79.         }
  80.         $pages \array_fill_keys($datanull);
  81.         foreach ($pageStructures as $pageStructure) {
  82.             $pages[$pageStructure->getUuid()] = $this->structureResolver->resolveProperties($pageStructure$propertyMap$locale);
  83.         }
  84.         return new ContentView(\array_values(\array_filter($pages)), ['ids' => $data]);
  85.     }
  86. }