<?php declare(strict_types=1);
namespace Ott\SaleCategory\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NavigationPageSubscriber implements EventSubscriberInterface
{
private SalesChannelRepositoryInterface $salutationRepository;
public function __construct(
SalesChannelRepositoryInterface $salutationRepository
)
{
$this->salutationRepository = $salutationRepository;
}
public static function getSubscribedEvents(): array
{
return [
NavigationPageLoadedEvent::class => 'onNavigationPageLoadedEvent',
];
}
public function onNavigationPageLoadedEvent(NavigationPageLoadedEvent $event): void
{
$salutationIds = [];
$salutationCollection = $this->salutationRepository->search(
new Criteria(),
$event->getSalesChannelContext()
)->getEntities();
foreach ($salutationCollection as $salutation) {
$salutationIds[$salutation->getSalutationKey()] = $salutation->getId();
}
$event->getPage()->addExtension('ottSalesChannelSalutations', new ArrayStruct(['data' => $salutationIds]));
}
}