custom/plugins/CogiTags/src/Subscriber/StorefrontSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\Tags\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class StorefrontSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var EntityRepositoryInterface
  11.      */
  12.     protected $tagRepository;
  13.     public function __construct(
  14.         EntityRepositoryInterface $tagRepository
  15.     )
  16.     {
  17.         $this->tagRepository $tagRepository;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent'
  23.         ];
  24.     }
  25.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event): void
  26.     {
  27.         $tagIds $event->getPage()->getProduct()->getTagIds();
  28.         if (empty($tagIds)) {
  29.             return;
  30.         }
  31.         $criteria = new Criteria($tagIds);
  32.         /** @var TagCollection $tags */
  33.         $tags $this->tagRepository->search($criteria$event->getContext())->getEntities();
  34.         $event->getPage()->getProduct()->addExtension('cogi-tags'$tags);
  35.     }
  36. }