vendor/store.shopware.com/netzpgallery6/src/Subscriber/FrontendSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpGallery6\Subscriber;
  3. use NetzpGallery6\Components\GalleryHelper;
  4. use NetzpGallery6\Core\Content\Gallery\GalleryDefinition;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class FrontendSubscriber implements EventSubscriberInterface
  12. {
  13.     private $container;
  14.     private $config;
  15.     private $requestStack;
  16.     private $helper;
  17.     public function __construct(
  18.         ContainerInterface $container,
  19.         SystemConfigService $config,
  20.         RequestStack $requestStack,
  21.         GalleryHelper $helper
  22.     ) {
  23.         $this->container $container;
  24.         $this->config $config;
  25.         $this->requestStack $requestStack;
  26.         $this->helper $helper;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductPageCriteriaEvent::class   => 'onProductCriteriaLoaded',
  32.             ProductPageLoadedEvent::class     => 'onProductPageLoaded'
  33.         ];
  34.     }
  35.     public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
  36.     {
  37.         $event->getCriteria()->addAssociation('galleries');
  38.         $event->getCriteria()->getAssociation('galleries')->addAssociation('gallerymedia');
  39.     }
  40.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  41.     {
  42.         $config $this->config->get('NetzpGallery6.config'$event->getSalesChannelContext()->getSalesChannel()->getId());
  43.         $galleries $event->getPage()->getProduct()->getExtension('galleries');
  44.         if(count($galleries) > 0) {
  45.             foreach($galleries as $gallery) {
  46.                 if($gallery->getType() == GalleryDefinition::TYPE_FOLDER) {
  47.                     $mediaFolderItems $this->helper->getMediaFromFolderId(
  48.                         $gallery->getMediaFolderId(),
  49.                         $event->getContext(),
  50.                         $config && array_key_exists('sortmode'$config) ? $config['sortmode'] : 0
  51.                     );
  52.                     $gallery->addExtension('mediafolder'$mediaFolderItems);
  53.                 }
  54.                 else if($gallery->getType() == GalleryDefinition::TYPE_TAGS) {
  55.                     $mediaTagsItems $this->helper->getMediaFromTags(
  56.                         array_filter(explode(','$gallery->getTags())),
  57.                         $event->getContext(),
  58.                         $config && array_key_exists('sortmode'$config) ? $config['sortmode'] : 0
  59.                     );
  60.                     $gallery->addExtension('mediafolder'$mediaTagsItems);
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }