<?php declare(strict_types=1);
namespace FourtwosixThemeCustomization\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class GetManufacturersSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productManufacturerRepository;
public function __construct(EntityRepositoryInterface $productManufacturerRepository)
{
$this->productManufacturerRepository = $productManufacturerRepository;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onStorefrontPageLoaded'
];
}
public function onStorefrontPageLoaded(PageLoadedEvent $event)
{
$page = $event->getPage();
$context = $event->getContext();
$extensions = $page->getExtensions();
$manufacturers = $this->getAllProductManufacturers($context);
$extensions['allManufacturers'] = $manufacturers->getElements();
$page->setExtensions($extensions);
}
private function getAllProductManufacturers(Context $context): EntitySearchResult
{
$criteria = new Criteria();
$criteria->addSorting(new FieldSorting('name'));
$productManufacturers = $this->productManufacturerRepository->search($criteria, $context);
return $productManufacturers;
}
}