<?php declare(strict_types=1);
namespace System4ShopTheme\Subscriber;
use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HeaderResponseSubscriber implements EventSubscriberInterface
{
/**
* @var NavigationLoaderInterface
*/
private NavigationLoaderInterface $navigationLoader;
private SystemConfigService $systemConfigService;
public function __construct(
NavigationLoaderInterface $navigationLoader,
SystemConfigService $systemConfigService
) {
$this->navigationLoader = $navigationLoader;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'addHeaderNavigation',
];
}
public function addHeaderNavigation(HeaderPageletLoadedEvent $event)
{
$rootId = $this->systemConfigService->get('System4ShopTheme.config.headerGroupCategory', $event->getSalesChannelContext()->getSalesChannelId());
$tree = null;
if ($rootId) {
$navigationId = $event->getRequest()->get('navigationId', $rootId);
$tree = $this->navigationLoader->load($navigationId, $event->getSalesChannelContext(), $rootId, 2);
$newTree = array_slice($tree->getTree(), 0, 7);
// $tree->setTree($newTree);
$tree->setTree($tree->getTree());
}
$event->getPagelet()->assign([
'customHeaderNavigation' => $tree
]);
}
}