<?php
declare(strict_types=1);
namespace LDSCustom\Subscriber;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class Frontend implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $manufacturerRepository;
public function __construct(EntityRepositoryInterface $manufacturerRepository)
{
$this->manufacturerRepository = $manufacturerRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'onPageLoaded'
];
}
/**
* @param HeaderPageletLoadedEvent $event
*/
public function onPageLoaded(HeaderPageletLoadedEvent $event): void
{
$context = $event->getSalesChannelContext()->getContext();
$event->getPagelet()->addArrayExtension('LdsManufacturers', $this->getManufacturers($context));
}
private function getManufacturers($context)
{
$criteria = new Criteria();
$criteria->setTitle(' manufacturer');
$criteria->addSorting(new FieldSorting('name'));
$criteria->addAssociation('media');
$manufacturers = $this->manufacturerRepository->search($criteria, $context)->getElements();
foreach ($manufacturers as $key => $manufacturer) {
if (!empty($manufacturer->getTranslated()['customFields']['cbaxManufacturerIsHidden'])) {
unset($manufacturers[$key]);
}
}
return $manufacturers;
}
}