<?php declare(strict_types=1);
namespace ThemeOkeOnline\Storefront\Event;
// namespace Okeonline\AssetsKeyValue\Storefront\Page;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StorefrontRenderEventSubscriber implements EventSubscriberInterface
{
/**
* @var GenericPageLoadedEvent
*/
private $event;
/**
* @var EntityRepository
*/
private $categoryRepository;
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $context;
public function __construct(
EntityRepository $categoryRepository,
SystemConfigService $systemConfigService
)
{
$this->categoryRepository = $categoryRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
'Shopware\Storefront\Event\StorefrontRenderEvent' => ['addCategoriesToPage', 0]
];
}
public function addCategoriesToPage(StorefrontRenderEvent $event): void
{
$this->event = $event;
$parameters = $event->getParameters();
// Check if page exists, if not this is probably something ajax.
if(!array_key_exists('page', $parameters))
return;
$page=$parameters['page'];
$context = $event->getContext();
$criteria = new Criteria();
$criteria->addAssociation('children.children');
$data = $this->categoryRepository->search($criteria, $context);
foreach($data as $item) {
$item->children->sortByPosition();
}
$page->addExtension('categories', $data);
return;
}
}