custom/plugins/NenoHeroSlider/src/Storefront/Subscriber/NavigationPageLoadedSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neno\HeroSlider\Storefront\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class NavigationPageLoadedSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     private $slideRepository;
  15.     public function __construct(EntityRepositoryInterface $slideRepository)
  16.     {
  17.         $this->slideRepository $slideRepository;
  18.     }
  19.     /**
  20.      * @inheritDoc
  21.      */
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             NavigationPageLoadedEvent::class => 'addHeroSliderData',
  26.         ];
  27.     }
  28.     public function addHeroSliderData(NavigationPageLoadedEvent $event): void
  29.     {
  30.         $request $event->getRequest();
  31.         $context $event->getSalesChannelContext();
  32.         $categoryId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  33.         if (!$categoryId) {
  34.             return;
  35.         }
  36.         $criteria = (new Criteria())
  37.             ->addAssociation('categories')
  38.             ->addFilter(new EqualsFilter('categories.id'$categoryId))
  39.             ->addSorting(new FieldSorting('priority'));
  40.         $slides $this->slideRepository
  41.             ->search($criteria$event->getContext())
  42.             ->getEntities();
  43.         $event->getPage()->addExtension('hero_slider_slides'$slides);
  44.     }
  45. }