custom/plugins/EmzPlatformShippingPaymentIcons/src/Subscriber/Frontend.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Emz\ShippingPaymentIcons\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;;
  11. class Frontend implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityRepositoryInterface
  15.      */
  16.     private $iconRepository;
  17.     /**
  18.      * @var EntityRepositoryInterface
  19.      */
  20.     private $mediaRepository;
  21.     public function __construct(
  22.         EntityRepositoryInterface $iconRepository,
  23.         EntityRepositoryInterface $mediaRepository
  24.     ) {
  25.         $this->iconRepository $iconRepository;
  26.         $this->mediaRepository $mediaRepository;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             FooterPageletLoadedEvent::class => 'onFooterLoaded',
  32.             ProductPageLoadedEvent::class => 'onProductLoaded'
  33.         ];
  34.     }
  35.     public function onFooterLoaded(FooterPageletLoadedEvent $event)
  36.     {
  37.         $criteria = new Criteria();
  38.         $criteria
  39.             ->addFilter(new EqualsFilter('activeFooter'true))
  40.             ->addAssociation('media');
  41.         $icons $this->getIcons($criteria$event->getContext());
  42.         $event->getPagelet()->addExtension('emzShippingPaymentIcons', new ArrayEntity(['wrapper' => $icons]));
  43.     }
  44.     public function onProductLoaded(ProductPageLoadedEvent $event)
  45.     {
  46.         $criteria = new Criteria();
  47.         $criteria
  48.             ->addFilter(new EqualsFilter('activeProduct'true))
  49.             ->addAssociation('media');
  50.         $icons $this->getIcons($criteria$event->getContext());
  51.         $event->getPage()->addExtension('emzShippingPaymentIcons', new ArrayEntity(['wrapper' => $icons]));
  52.     }
  53.     private function getIcons(Criteria $criteriaContext $context)
  54.     {
  55.         $icons $this->iconRepository->search($criteria$context)->getElements();
  56.         if (!$icons) {
  57.             return[];
  58.         }
  59.         $returnIcons = [];
  60.         foreach ($icons as $icon) {
  61.             if (!$icon) {
  62.                 continue;
  63.             }
  64.             $returnIcons[] = [
  65.                 'src' => $icon->getMedia(),
  66.                 'title' => $icon->getTranslation('title'),
  67.                 'alt' => $icon->getTranslation('alt')
  68.             ];
  69.         }
  70.         return $returnIcons;
  71.     }
  72. }