custom/plugins/theme/src/Subscriber/GenericPageLoadedSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DonCarneTheme\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. class GenericPageLoadedSubscriber implements EventSubscriberInterface
  12. {
  13.     private SystemConfigService $systemConfigService;
  14.     private EntityRepository $categoryRepository;
  15.     public function __construct(
  16.         SystemConfigService $systemConfigService,
  17.         EntityRepository $categoryRepository
  18.     )
  19.     {
  20.         $this->systemConfigService $systemConfigService;
  21.         $this->categoryRepository $categoryRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             GenericPageLoadedEvent::class => 'addInfo'
  27.         ];
  28.     }
  29.     public function addInfo(GenericPageLoadedEvent $event)
  30.     {
  31.         $this->addTopBar($event);
  32.         $this->addMenuSpecialCategories($event);
  33.     }
  34.     private function addTopBar(GenericPageLoadedEvent $event)
  35.     {
  36.         
  37.         $config $this->systemConfigService->get('DonCarneTheme.config');
  38.         if(!$config){
  39.             return;
  40.         }
  41.         
  42.         $keys = ["topbarCategory1""topbarCategory2""topbarCategory3""topbarCategory4"];
  43.         $categoryIds $this->addConfigCategory($config$keys);
  44.         $topbarCategories null;
  45.         
  46.         if(!empty($categoryIds)){
  47.             $topbarCategories $this->getCategories($categoryIds$event->getContext());
  48.         }
  49.         if($topbarCategories) {
  50.             $event->getPage()->addExtension(
  51.                 'topbar',
  52.                 new ArrayStruct($topbarCategories)
  53.             );
  54.         }
  55.     }
  56.     private function addConfigCategory(array $config, array $keys): array
  57.     {
  58.         $categoryIds = [];
  59.         foreach($keys as $key){
  60.             if(isset($config[$key])){
  61.                 $categoryIds[] = $config[$key];
  62.             }
  63.         }
  64.         return $categoryIds;
  65.     }
  66.     private function getCategories(array $categoryIdsContext $context): array
  67.     {
  68.         $criteria = (new Criteria())
  69.             ->addFilter(new EqualsAnyFilter('id'$categoryIds));
  70.         
  71.         return $this->categoryRepository->search($criteria$context)->getElements();
  72.     }
  73.     private function addMenuSpecialCategories(GenericPageLoadedEvent $event)
  74.     {
  75.         $header =  $event->getPage()->getHeader();
  76.         if(!$header){
  77.             return;
  78.         }
  79.         $categories $header->getNavigation()->getTree();
  80.         if(!$categories){
  81.             return;
  82.         }
  83.         $resolvedCategories = [];
  84.         $extraCategories false;
  85.         $showExtraCategories false;
  86.         
  87.         foreach ($categories as $category){
  88.             foreach($category->getChildren() as $subcategory){
  89.                 $customFields $subcategory->getCategory()->getCustomFields();
  90.                 if($customFields && isset($customFields['doncarne_category_special_menu_display'])){
  91.                     $customFields $subcategory->getCategory()->getCustomFields();
  92.                     $categoryIds = isset($customFields['doncarne_category_custom_fields_special_menu_extra_categories']) ? $customFields['doncarne_category_custom_fields_special_menu_extra_categories'] : [];
  93.                     if(!empty($categoryIds)){
  94.                         $extraCategories $this->getCategories($categoryIds$event->getContext());
  95.                         if($extraCategories) {
  96.                             $resolvedCategories array_merge($resolvedCategories$extraCategories);
  97.                             $showExtraCategories true;
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         if($showExtraCategories){
  104.             $event->getPage()->getHeader()->addExtension(
  105.                 'menuExtraCategories',
  106.                 new ArrayStruct($resolvedCategories)
  107.             );
  108.         }
  109.     }
  110. }