custom/plugins/ThemeOkeonline/src/Storefront/Event/StorefrontRenderEventSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ThemeOkeOnline\Storefront\Event;
  3. // namespace Okeonline\AssetsKeyValue\Storefront\Page;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Struct\ArrayEntity;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Event\StorefrontRenderEvent;
  13. use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\Config\Definition\Exception\Exception;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class StorefrontRenderEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var GenericPageLoadedEvent
  21.      */
  22.     private $event;
  23.     /**
  24.      * @var EntityRepository
  25.      */
  26.     private $categoryRepository;
  27.     /**
  28.      * @var SystemConfigService
  29.      */
  30.     private $systemConfigService;
  31.     private $context;
  32.     public function __construct(
  33.         EntityRepository $categoryRepository,
  34.         SystemConfigService $systemConfigService
  35.     )
  36.     {
  37.         $this->categoryRepository $categoryRepository;
  38.         $this->systemConfigService $systemConfigService;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             'Shopware\Storefront\Event\StorefrontRenderEvent' => ['addCategoriesToPage'0]
  44.         ];
  45.     }
  46.     public function addCategoriesToPage(StorefrontRenderEvent $event): void
  47.     {
  48.         $this->event $event;
  49.         $parameters $event->getParameters();
  50.         // Check if page exists, if not this is probably something ajax.
  51.         if(!array_key_exists('page'$parameters))
  52.             return;
  53.         $page=$parameters['page'];
  54.         $context $event->getContext();
  55.      
  56.         $criteria = new Criteria();
  57.         $criteria->addAssociation('children.children');
  58.         $data $this->categoryRepository->search($criteria$context);
  59.         foreach($data as $item) {
  60.             $item->children->sortByPosition();
  61.         }
  62.         $page->addExtension('categories'$data);
  63.         return;
  64.     }   
  65. }