<?php
declare(strict_types=1);
namespace System4ShopTheme\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class StorefrontSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $paymentIconsRepo;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $paymentIconsRepo
) {
$this->systemConfigService = $systemConfigService;
$this->paymentIconsRepo = $paymentIconsRepo;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$criteria = new Criteria();
$criteria->addAssociation('media');
$criteria->addFilter(new EqualsFilter('active', 1));
$criteria->addSorting(new FieldSorting('position', FieldSorting::ASCENDING));
$paymentIcons = $this->paymentIconsRepo->search($criteria, Context::createDefaultContext());
$event->setParameter('paymentIcons', $paymentIcons);
}
}