vendor/store.shopware.com/moorlfoundation/src/Core/System/EntityListingExtension.php line 212

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFoundation\Core\System;
  3. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  4. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
  5. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingRouteResponse;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. class EntityListingExtension implements EntityListingInterface
  18. {
  19.     protected EntityDefinition $entityDefinition;
  20.     protected SystemConfigService $systemConfigService;
  21.     protected SalesChannelContext $salesChannelContext;
  22.     protected ?ProductListingResultEvent $event null;
  23.     protected EventDispatcherInterface $eventDispatcher;
  24.     protected Request $request;
  25.     protected ?SalesChannelRepositoryInterface $salesChannelRepository null;
  26.     protected ?Filter $filter null;
  27.     protected ?string $route;
  28.     public function __construct(
  29.         ?SalesChannelRepositoryInterface $salesChannelRepository null
  30.     )
  31.     {
  32.         $this->salesChannelRepository $salesChannelRepository;
  33.     }
  34.     public function isWidget(): bool
  35.     {
  36.         return in_array($this->route, [
  37.             "widgets.search.filter",
  38.             "widgets.search.pagelet.v2",
  39.         ]);
  40.     }
  41.     public function isSearch(): bool
  42.     {
  43.         return in_array($this->route, [
  44.             "frontend.search.page",
  45.             "widgets.search.pagelet.v2"
  46.         ]);
  47.     }
  48.     public function isSuggest(): bool
  49.     {
  50.         return in_array($this->route, [
  51.             "frontend.search.suggest"
  52.         ]);
  53.     }
  54.     public function getElementConfig(): array
  55.     {
  56.         if ($this->isSuggest()) {
  57.             return [
  58.                 'listingLayout' => ['value' => 'search-suggest']
  59.             ];
  60.         }
  61.         return [
  62.             'listingSource' => ['value' => 'auto'],
  63.             'listingLayout' => ['value' => 'grid'],
  64.             'itemLayout' => ['value' => 'overlay'],
  65.             'displayMode' => ['value' => 'cover'],
  66.             'textAlign' => ['value' => 'left'],
  67.             'gapSize' => ['value' => '20px'],
  68.             'itemWidth' => ['value' => '300px'],
  69.             'itemHeight' => ['value' => '400px'],
  70.             'itemHasBorder' => ['value' => false],
  71.             'contentPadding' => ['value' => '20px'],
  72.             'hasButton' => ['value' => true],
  73.             'buttonClass' => ['value' => 'btn btn-dark'],
  74.             'buttonLabel' => ['value' => null],
  75.         ];
  76.     }
  77.     /**
  78.      * @param SalesChannelContext $salesChannelContext
  79.      */
  80.     public function setSalesChannelContext(SalesChannelContext $salesChannelContext): void
  81.     {
  82.         $this->salesChannelContext $salesChannelContext;
  83.     }
  84.     /**
  85.      * @param Request $request
  86.      */
  87.     public function setRequest(Request $request): void
  88.     {
  89.         $this->request $request;
  90.         $this->route $request->attributes->get('_route');
  91.     }
  92.     /**
  93.      * @param ProductListingResultEvent $event
  94.      */
  95.     public function setEvent(ProductListingResultEvent $event): void
  96.     {
  97.         $this->event $event;
  98.         $this->salesChannelContext $event->getSalesChannelContext();
  99.         $this->setRequest($event->getRequest());
  100.     }
  101.     /**
  102.      * @param EventDispatcherInterface $eventDispatcher
  103.      */
  104.     public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
  105.     {
  106.         $this->eventDispatcher $eventDispatcher;
  107.     }
  108.     /**
  109.      * @return SalesChannelRepositoryInterface|null
  110.      */
  111.     public function getSalesChannelRepository(): ?SalesChannelRepositoryInterface
  112.     {
  113.         return $this->salesChannelRepository;
  114.     }
  115.     /**
  116.      * @param SystemConfigService $systemConfigService
  117.      */
  118.     public function setSystemConfigService(SystemConfigService $systemConfigService): void
  119.     {
  120.         $this->systemConfigService $systemConfigService;
  121.     }
  122.     public function getEntityName(): string
  123.     {
  124.         return "";
  125.     }
  126.     public function getTitle(): string
  127.     {
  128.         return "";
  129.     }
  130.     public function processSearchResult(ProductListingResult $searchResult): void
  131.     {
  132.     }
  133.     public function processCriteria(Criteria $criteria): void
  134.     {
  135.     }
  136.     public function isActive(): bool
  137.     {
  138.         return true;
  139.     }
  140.     public function inheritCriteria(): bool
  141.     {
  142.         return false;
  143.     }
  144.     public function getLimit(): int
  145.     {
  146.         return 6;
  147.     }
  148.     public function getTerm(string $search): string
  149.     {
  150.         return $search;
  151.     }
  152.     public function getSnippet(): ?string
  153.     {
  154.         return null;
  155.     }
  156.     public function getTemplatePath(): ?string
  157.     {
  158.         return null;
  159.     }
  160.     public function listingLoader(Criteria $origin): EntitySearchResult
  161.     {
  162.         $criteria = clone $origin;
  163.         $this->salesChannelContext->getContext()->addState(Context::STATE_ELASTICSEARCH_AWARE);
  164.         $ids $this->salesChannelRepository->searchIds($criteria$this->salesChannelContext);
  165.         $aggregations $this->salesChannelRepository->aggregate($criteria$this->salesChannelContext);
  166.         if (empty($ids->getIds())) {
  167.             return new EntitySearchResult(
  168.                 $this->getEntityName(),
  169.                 0,
  170.                 new EntityCollection(),
  171.                 $aggregations,
  172.                 $origin,
  173.                 $this->salesChannelContext->getContext()
  174.             );
  175.         }
  176.         $entities $this->salesChannelRepository->search($criteria$this->salesChannelContext);
  177.         $result = new EntitySearchResult($this->getEntityName(), $ids->getTotal(), $entities->getEntities(), $aggregations$origin$this->salesChannelContext->getContext());
  178.         $result->addState(...$ids->getStates());
  179.         return $result;
  180.     }
  181.     public function listingRoute(Criteria $criteria, ?string $categoryId null): ProductListingRouteResponse
  182.     {
  183.         $this->processCriteria($criteria);
  184.         $criteria->setTitle($this->getTitle());
  185.         $entities $this->listingLoader($criteria);
  186.         /** @var ProductListingResult $result */
  187.         $result ProductListingResult::createFrom($entities);
  188.         $result->addState(...$entities->getStates());
  189.         $result->assign([
  190.             'elementConfig' => $this->getElementConfig()
  191.         ]);
  192.         $result->addCurrentFilter('navigationId'$categoryId);
  193.         $this->processSearchResult($result);
  194.         return new ProductListingRouteResponse($result);
  195.     }
  196. }