vendor/shopware/core/Content/Product/Cms/ProductListingCmsElementResolver.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\Cms;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  4. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  5. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  6. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  7. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  8. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  9. use Shopware\Core\Content\Product\SalesChannel\Listing\AbstractProductListingRoute;
  10. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingFeaturesSubscriber;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\Log\Package;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\HttpFoundation\Request;
  15. #[Package('inventory')]
  16. class ProductListingCmsElementResolver extends AbstractCmsElementResolver
  17. {
  18.     private AbstractProductListingRoute $listingRoute;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(AbstractProductListingRoute $listingRoute)
  23.     {
  24.         $this->listingRoute $listingRoute;
  25.     }
  26.     public function getType(): string
  27.     {
  28.         return 'product-listing';
  29.     }
  30.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  31.     {
  32.         return null;
  33.     }
  34.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  35.     {
  36.         $data = new ProductListingStruct();
  37.         $slot->setData($data);
  38.         $request $resolverContext->getRequest();
  39.         $context $resolverContext->getSalesChannelContext();
  40.         $this->restrictFilters($slot$request);
  41.         if ($this->isCustomSorting($slot)) {
  42.             $this->restrictSortings($request$slot);
  43.             $this->addDefaultSorting($request$slot);
  44.         }
  45.         $navigationId $this->getNavigationId($request$context);
  46.         $criteria = new Criteria();
  47.         $criteria->setTitle('cms::product-listing');
  48.         $listing $this->listingRoute
  49.             ->load($navigationId$request$context$criteria)
  50.             ->getResult();
  51.         $data->setListing($listing);
  52.     }
  53.     private function getNavigationId(Request $requestSalesChannelContext $salesChannelContext): string
  54.     {
  55.         if ($navigationId $request->get('navigationId')) {
  56.             return $navigationId;
  57.         }
  58.         $params $request->attributes->get('_route_params');
  59.         if ($params && isset($params['navigationId'])) {
  60.             return $params['navigationId'];
  61.         }
  62.         return $salesChannelContext->getSalesChannel()->getNavigationCategoryId();
  63.     }
  64.     private function isCustomSorting(CmsSlotEntity $slot): bool
  65.     {
  66.         $config $slot->getTranslation('config');
  67.         if ($config && isset($config['useCustomSorting']) && isset($config['useCustomSorting']['value'])) {
  68.             return $config['useCustomSorting']['value'];
  69.         }
  70.         return false;
  71.     }
  72.     private function addDefaultSorting(Request $requestCmsSlotEntity $slot): void
  73.     {
  74.         if ($request->get('order')) {
  75.             return;
  76.         }
  77.         $config $slot->getTranslation('config');
  78.         if ($config && isset($config['defaultSorting']) && isset($config['defaultSorting']['value']) && $config['defaultSorting']['value']) {
  79.             $request->request->set('order'$config['defaultSorting']['value']);
  80.             return;
  81.         }
  82.         // if we have no specific order given at this point, set the order to be the highest's priority available sorting
  83.         if ($request->get('availableSortings')) {
  84.             $availableSortings $request->get('availableSortings');
  85.             arsort($availableSortings\SORT_DESC \SORT_NUMERIC);
  86.             $request->request->set('order'array_key_first($availableSortings));
  87.         }
  88.     }
  89.     private function restrictSortings(Request $requestCmsSlotEntity $slot): void
  90.     {
  91.         $config $slot->getTranslation('config');
  92.         if (!$config || !isset($config['availableSortings']) || !isset($config['availableSortings']['value'])) {
  93.             return;
  94.         }
  95.         $request->request->set('availableSortings'$config['availableSortings']['value']);
  96.     }
  97.     private function restrictFilters(CmsSlotEntity $slotRequest $request): void
  98.     {
  99.         // setup the default behavior
  100.         $defaults = ['manufacturer-filter''rating-filter''shipping-free-filter''price-filter''property-filter'];
  101.         $request->request->set(ProductListingFeaturesSubscriber::PROPERTY_GROUP_IDS_REQUEST_PARAMnull);
  102.         $config $slot->get('config');
  103.         if (isset($config['propertyWhitelist']['value']) && \count($config['propertyWhitelist']['value']) > 0) {
  104.             $request->request->set(ProductListingFeaturesSubscriber::PROPERTY_GROUP_IDS_REQUEST_PARAM$config['propertyWhitelist']['value']);
  105.         }
  106.         if (!isset($config['filters']['value'])) {
  107.             return;
  108.         }
  109.         // apply config settings
  110.         $config explode(','$config['filters']['value']);
  111.         foreach ($defaults as $filter) {
  112.             if (\in_array($filter$configtrue)) {
  113.                 continue;
  114.             }
  115.             $request->request->set($filterfalse);
  116.         }
  117.     }
  118. }