vendor/store.shopware.com/h1webblog/src/Blog/Cms/BlogListingCmsElementResolver.php line 112

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace H1web\Blog\Blog\Cms;
  3. use H1web\Blog\Blog\Events\BlogListingResultEvent;
  4. use H1web\Blog\Blog\Listing\BlogListingFeaturesSubscriber;
  5. use H1web\Blog\Blog\Listing\BlogListingGateway;
  6. use H1web\Blog\Blog\Listing\BlogListingResult;
  7. use H1web\Blog\Blog\SalesChannel\BlogAvailableFilter;
  8. use H1web\Blog\Blog\SalesChannel\SalesChannelBlogDefinition;
  9. use H1web\Blog\Blog\Struct\BlogListingStruct;
  10. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  11. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  12. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  13. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  14. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. class BlogListingCmsElementResolver extends AbstractCmsElementResolver
  22. {
  23.     /**
  24.      * @var BlogListingGateway
  25.      */
  26.     private $listingGateway;
  27.     /**
  28.      * @var BlogListingFeaturesSubscriber
  29.      */
  30.     private $listingSubscriber;
  31.     /**
  32.      * @var EventDispatcherInterface
  33.      */
  34.     private $dispatcher;
  35.     public function __construct(BlogListingGateway $listingGatewayBlogListingFeaturesSubscriber $listingSubscriberEventDispatcherInterface $dispatcher)
  36.     {
  37.         $this->listingGateway $listingGateway;
  38.         $this->listingSubscriber $listingSubscriber;
  39.         $this->dispatcher $dispatcher;
  40.     }
  41.     public function getType(): string
  42.     {
  43.         return 'h1webblog-listing';
  44.     }
  45.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  46.     {
  47.         $config $slot->getFieldConfig();
  48.         $onlyRecentConfig $config->get('onlyRecent');
  49.         $filterTagsConfig $config->get('filterTags');
  50.         if (
  51.             (!$filterTagsConfig || empty($filterTagsConfig->getValue()))
  52.             && (!$onlyRecentConfig || intval($onlyRecentConfig->getValue()) === 0)
  53.         ) {
  54.             return null;
  55.         }
  56.         $criteria = new Criteria();
  57.         $criteria->addFilter(new BlogAvailableFilter($resolverContext->getSalesChannelContext()->getSalesChannelId()))
  58.             ->addAssociation('tags')
  59.             ->addAssociation('properties')
  60.             ->addAssociation('options')
  61.             ->addAssociation('visibilities')
  62.             ->addSorting(new FieldSorting('publishedAt'FieldSorting::DESCENDING));
  63.         $criteria->addAggregation(new TermsAggregation('properties''h1webblog_blog.properties.id'));
  64.         $criteria->addAggregation(new TermsAggregation('options''h1webblog_blog.options.id'));
  65.         if (intval($onlyRecentConfig->getValue()) !== 0) {
  66.             $criteria->setLimit(intval($onlyRecentConfig->getValue()));
  67.         }
  68.         if (!$criteria->getLimit()) {
  69.             $criteria->setLimit($this->listingSubscriber->getDefaultPageSize());
  70.         }
  71.         if ($filterTagsConfig && !empty($filterTagsConfig->getValue())) {
  72.             $criteria->addFilter(new EqualsAnyFilter('tagIds'$filterTagsConfig->getValue()));
  73.         }
  74.         $criteriaCollection = new CriteriaCollection();
  75.         $criteriaCollection->add('blog_slot_' $slot->getUniqueIdentifier(), SalesChannelBlogDefinition::class, $criteria);
  76.         return $criteriaCollection;
  77.     }
  78.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  79.     {
  80.         $data = new BlogListingStruct();
  81.         $slot->setData($data);
  82.         /** @var EntitySearchResult|null $searchResult */
  83.         $searchResult $result->get('blog_slot_' $slot->getUniqueIdentifier());
  84.         if ($searchResult) {
  85.             $this->dispatchListingEvent($resolverContext$searchResult);
  86.         }
  87.         $listing $searchResult ??
  88.             $this->listingGateway->search($resolverContext->getRequest(), $resolverContext->getSalesChannelContext());
  89.         $data->setListing($listing);
  90.     }
  91.     private function dispatchListingEvent(ResolverContext $resolverContextEntitySearchResult $result): void
  92.     {
  93.         $this->dispatcher->dispatch(
  94.             new BlogListingResultEvent(
  95.                 $resolverContext->getRequest(),
  96.                 BlogListingResult::createFrom($result),
  97.                 $resolverContext->getSalesChannelContext()
  98.             )
  99.         );
  100.     }
  101. }