custom/plugins/GbmedCustomFieldsFilter/src/Subscriber/Filter.php line 73

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedCustomFieldsFilter
  12.  * @copyright      Copyright (c) 2023, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\CustomFieldsFilter\Subscriber;
  18. use Gbmed\CustomFieldsFilter\Components\Filter\FilterAbstract;
  19. use Gbmed\CustomFieldsFilter\Core\Content\CustomFieldsFilter\CustomFieldsFilterEntity;
  20. use Gbmed\CustomFieldsFilter\Service\Filter as ServiceFilter;
  21. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  22. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  23. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\BucketResult;
  25. use Shopware\Core\Framework\Struct\ArrayStruct;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. class Filter implements EventSubscriberInterface
  28. {
  29.     private ServiceFilter $serviceFilter;
  30.     /** @var CustomFieldsFilterEntity[] */
  31.     private array $customFieldsFilters = [];
  32.     public function __construct(ServiceFilter $serviceFilter)
  33.     {
  34.         $this->serviceFilter $serviceFilter;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
  40.             ProductListingCollectFilterEvent::class => 'addFilter',
  41.             ProductListingResultEvent::class => 'onProductListingResultEvent',
  42.         ];
  43.     }
  44.     public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event)
  45.     {
  46.         $event->getCriteria()->addAssociation('translations');
  47.     }
  48.     public function addFilter(ProductListingCollectFilterEvent $event): void
  49.     {
  50.         $customFieldsFilters $this->serviceFilter->getActiveFilters($event->getContext());
  51.         /**
  52.          * @var CustomFieldsFilterEntity $customFieldsFilter
  53.          */
  54.         foreach ($customFieldsFilters as $customFieldsFilter) {
  55.             $filterType $this->serviceFilter->getCollection()->get($customFieldsFilter->getType());
  56.             if (!$filterType) {
  57.                 continue;
  58.             }
  59.             $this->customFieldsFilters[FilterAbstract::NAME_PREFIX $customFieldsFilter->getId()] = $customFieldsFilter;
  60.             $filterType->setProductListingEvent($event)
  61.                 ->setCustomFieldsFilter($customFieldsFilter)
  62.                 ->addProductListing($event);
  63.         }
  64.     }
  65.     public function onProductListingResultEvent(ProductListingResultEvent $event)
  66.     {
  67.         $aggregations $event->getResult()->getAggregations();
  68.         $list = [];
  69.         /*** @var BucketResult $aggregation */
  70.         foreach ($aggregations as $aggregationName => $aggregation) {
  71.             if (!strstr($aggregationNameFilterAbstract::NAME_PREFIX)
  72.                 || !isset($this->customFieldsFilters[$aggregation->getName()])
  73.             ) {
  74.                 continue;
  75.             }
  76.             $list[] = [
  77.                 'aggregation' => $aggregation,
  78.                 'customFieldsFilters' => $this->customFieldsFilters[$aggregation->getName()]
  79.             ];
  80.         }
  81.         usort($list, fn($a$b) => $a['customFieldsFilters']->getPosition() <=> $b['customFieldsFilters']->getPosition());
  82.         $event->getResult()->addExtension('gbmedCustomFieldsFilters', new ArrayStruct($list));
  83.     }
  84. }