custom/plugins/KsAttachments/src/Subscriber/PageSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ks\Attachments\Subscriber;
  4. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Ks\Attachments\Service\AttachmentService;
  13. /**
  14.  * Class PageSubscriber
  15.  * @package Ks\Attachments\Subscriber
  16.  */
  17. class PageSubscriber implements EventSubscriberInterface
  18. {
  19.     const ENABLE_STATUS 1;
  20.     private $attachmentsRepository;
  21.     private $attachmentService;
  22.     /**
  23.      * PageSubscriber constructor.
  24.      * @param EntityRepositoryInterface $attachmentsRepository
  25.      * @param AttachmentService $attachmentService
  26.      */
  27.     public function __construct(
  28.         EntityRepositoryInterface $attachmentsRepository,
  29.         AttachmentService $attachmentService
  30.     ) {
  31.         $this->attachmentsRepository $attachmentsRepository;
  32.         $this->attachmentService $attachmentService;
  33.     }
  34.     /**
  35.      * @return array|array[]
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             ProductPageLoadedEvent::class => ['onProductPageLoaded'100],
  41.             ProductListingResultEvent::class => 'onProductListingResult'
  42.         ];
  43.     }
  44.     /**
  45.      * @param ProductPageLoadedEvent $event
  46.      */
  47.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  48.     {
  49.         $attachments $this->attachmentService->getAttachmentForLoadedPage($event);
  50.         if ($attachments->getTotal() > 0) {
  51.             $attachments $attachments->getElements();
  52.             $event->getPage()->getProduct()->assign(
  53.                 [
  54.                     'KsAttachments' => [
  55.                         'attachments' => $attachments
  56.                     ]
  57.                 ]
  58.             );
  59.         } else {
  60.             return;
  61.         }
  62.     }
  63.     /**
  64.      * @param ProductListingResultEvent $event
  65.      */
  66.     public function onProductListingResult(ProductListingResultEvent $event)
  67.     {
  68.         $route $event->getRequest()->attributes->get('_route');
  69.         $products $event->getResult()->getEntities();
  70.         // fourtwosix-edit: customer login check removed
  71.         $isLoggedIn false;
  72.         $currentCustomerGroupId null;
  73.         foreach ($products as $product) {
  74.             $attachmentId $this->attachmentService->getMatchAttachmentId(
  75.                 $route,
  76.                 $currentCustomerGroupId,
  77.                 $isLoggedIn,
  78.                 $product->getId()
  79.             );
  80.             $criteria = new Criteria();
  81.             $criteria->addFilter(new EqualsAnyFilter('id'$attachmentId));
  82.             $criteria->addFilter(new EqualsFilter('display'"3"));
  83.             $criteria->addSorting(new FieldSorting('position'FieldSorting::ASCENDING));
  84.             $attachments $this->attachmentsRepository->search($criteria$event->getContext());
  85.             if ($attachments->getTotal() > 0) {
  86.                 $product->assign(
  87.                     [
  88.                         'KsAttachments' => [
  89.                             'attachments' => $attachments
  90.                         ]
  91.                     ]
  92.                 );
  93.             }
  94.         }
  95.     }
  96. }