<?php
declare(strict_types=1);
namespace NetInventors\NetiNextStoreLocator\Subscriber;
use NetInventors\NetiNextStoreLocator\Struct\PluginConfigStruct;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HeaderPageletLoaderSubscriber implements EventSubscriberInterface
{
private PluginConfigStruct $pluginConfig;
private EntityRepositoryInterface $categoryRepository;
public function __construct(
PluginConfigStruct $pluginConfig,
EntityRepositoryInterface $categoryRepository
) {
$this->pluginConfig = $pluginConfig;
$this->categoryRepository = $categoryRepository;
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'onLoaded',
];
}
public function onLoaded(HeaderPageletLoadedEvent $event): void
{
$page = $event->getPagelet();
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (
$route === 'frontend.store_locator.index'
&& $categoryId = $this->pluginConfig->getBreadcrumbCategory()
) {
$criteria = new Criteria([ $categoryId ]);
$result = $this->categoryRepository->search($criteria, $event->getContext());
$category = $result->first();
if ($category instanceof CategoryEntity) {
$page->getNavigation()->setActive($category);
}
}
}
}