custom/plugins/S360Experts/src/Storefront/Page/Navigation/Subscriber/NavigationPageSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace S360Experts\Storefront\Page\Navigation\Subscriber;
  3. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
  11. use S360Experts\Core\Content\Expert\ExpertCollection;
  12. class NavigationPageSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     /**
  19.      * @var EntityRepository
  20.      */
  21.     private $expertRepository;
  22.     public function __construct(
  23.         SystemConfigService $systemConfigService
  24.         EntityRepository $expertRepository,
  25.         EntityRepository $categoryRepository
  26.     )   {
  27.         $this->expertRepository $expertRepository;
  28.         $this->systemConfigService $systemConfigService;
  29.         $this->categoryRepository $categoryRepository;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded'
  35.         ];
  36.     }
  37.     /**
  38.      * @param NavigationPageLoadedEvent $event
  39.      * @return void
  40.      */
  41.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event): void
  42.     {
  43.         // /navigation/$currentCategoryId
  44.         $path $event->getRequest()->getPathInfo();
  45.         // basic collection
  46.         $s360Experts = new ExpertCollection;
  47.         // make sure we're in a navigation
  48.         if (false === strpos($path'navigation')) {
  49.             return;
  50.         }
  51.         // Get last subCategory id, in case very deep navigations might have
  52.         // the entire path as, i.e. /navigation/$id/$id/$id
  53.         $explodedPath explode('/'$path);
  54.         $currentCategoryId $explodedPath[count($explodedPath) - 1];
  55.         $criteria = new Criteria();
  56.         $criteria->addAssociation('categories');
  57.         $allExperts $this->expertRepository->search($criteria$event->getContext());
  58.         foreach($allExperts as $expert) {
  59.             if($expert->getCategories()->has($currentCategoryId)) {
  60.                 $s360Experts->add($expert);
  61.             }
  62.         }
  63.         if($this->systemConfigService->get('S360Experts.config.inheritFromParent') && empty($s360Experts->getElements())) { // not sure if i should just use ->getTotal() here
  64.             // find parent categories here and iterate through them in reverse hierarchy until we find at least 1 expert
  65.             $criteria = new Criteria();
  66.             $criteria->addFilter(new EqualsFilter('id'$currentCategoryId));
  67.             $currentCategory $this->categoryRepository->search($criteria$event->getContext())->first();
  68.             $categoryTree array_reverse(explode('|'$currentCategory->getPath()));
  69.             $categoryTree array_filter($categoryTree, fn($value) => !is_null($value) && $value !== ''); // remove empty elements
  70.             array_pop($categoryTree); // without root category
  71.             foreach($allExperts as $expert) {
  72.                 if(empty($s360Experts->getElements())) {
  73.                     foreach($categoryTree as $categoryId) {
  74.                         if($expert->getCategories()->has($categoryId)) {
  75.                             $s360Experts->add($expert);
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         // this is the base entities we assign to my page
  82.         if(!empty($s360Experts->getElements())) {
  83.             $event->getPage()->addExtension('s360_experts'$s360Experts);
  84.         }
  85.         // this is their images
  86.         $s360ExpertImages = [];
  87.         foreach($s360Experts as $s360Expert) {
  88.             if(!empty($s360Expert->getImage())) {
  89.                 $s360ExpertImages[] = $s360Expert->getImage();
  90.             }
  91.         }
  92.         $event->getPage()->addExtension('s360_experts_images', new ArrayStruct($s360ExpertImages));
  93.     }
  94. }