<?php declare(strict_types = 1);
namespace GrimmTheme\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Content\Category\CategoryCollection;
use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
use Shopware\Core\Content\Category\Tree\Tree;
use Shopware\Core\Content\Category\Tree\TreeItem;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\System\Language\SalesChannel\AbstractLanguageRoute;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Event\RouteRequest\LanguageRouteRequestEvent;
use Shopware\Core\System\Language\LanguageCollection;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
class TopBarSubscriber implements EventSubscriberInterface {
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var NavigationLoaderInterface
*/
private $navigationLoader;
/**
* @var AbstractLanguageRoute
*/
private $languageRoute;
public function __construct(
SystemConfigService $systemConfigService,
NavigationLoaderInterface $navigationLoader,
AbstractLanguageRoute $languageRoute
) {
$this->systemConfigService = $systemConfigService;
$this->navigationLoader = $navigationLoader;
$this->languageRoute = $languageRoute;
}
public static function getSubscribedEvents()
{
return [
GenericPageLoadedEvent::class => 'onHeaderPageletLoadedEvent'
];
}
public function onHeaderPageletLoadedEvent(GenericPageLoadedEvent $event)
{
$topBarParentCategoryId = $this->systemConfigService->get('GrimmTheme.config.topBarLinks');
if(!$topBarParentCategoryId){
return;
}
$navigationTree = $this->getTree($event->getSalesChannelContext(), $topBarParentCategoryId);
$event->getPage()->addExtension(
'grimmTopbar',
$navigationTree
);
}
private function getTree(SalesChannelContext $context, string $topBarParentCategoryId): Tree
{
return $this->navigationLoader->load($topBarParentCategoryId, $context, $topBarParentCategoryId, 1);
}
}