<?php declare(strict_types=1);
namespace Neno\HeroSlider\Storefront\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NavigationPageLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $slideRepository;
public function __construct(EntityRepositoryInterface $slideRepository)
{
$this->slideRepository = $slideRepository;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
NavigationPageLoadedEvent::class => 'addHeroSliderData',
];
}
public function addHeroSliderData(NavigationPageLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$categoryId = $request->get('navigationId', $context->getSalesChannel()->getNavigationCategoryId());
if (!$categoryId) {
return;
}
$criteria = (new Criteria())
->addAssociation('categories')
->addFilter(new EqualsFilter('categories.id', $categoryId))
->addSorting(new FieldSorting('priority'));
$slides = $this->slideRepository
->search($criteria, $event->getContext())
->getEntities();
$event->getPage()->addExtension('hero_slider_slides', $slides);
}
}