custom/plugins/WynCustomerTheme/src/Subscriber/NavigationSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wyn\CustomerTheme\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  5. use Shopware\Core\Content\Media\MediaCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\Struct\ArrayStruct;
  12. use Shopware\Core\Framework\Struct\ArrayStruct as ArrayStructAlias;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Content\Product\ProductEvents;
  15. class NavigationSubscriber implements EventSubscriberInterface
  16. {
  17.   private EntityRepositoryInterface $mediaRepository;
  18.   /**
  19.    * @param EntityRepositoryInterface $mediaRepository
  20.    */
  21.   public function __construct(EntityRepositoryInterface $mediaRepository)
  22.   {
  23.     $this->mediaRepository $mediaRepository;
  24.   }
  25.   public static function getSubscribedEvents(): array
  26.   {
  27.     // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  28.     return [
  29.       NavigationLoadedEvent::class => 'onNavigationLoaded'
  30.     ];
  31.   }
  32.   public function onNavigationLoaded(NavigationLoadedEvent $event)
  33.   {
  34.     $navigationTree $event->getNavigation()->getTree();
  35.     $categoryIconMediaIds = [];
  36.     foreach($navigationTree as $treeItem) {
  37.       $children $treeItem->getChildren();
  38.       /** @var CategoryEntity $child */
  39.       foreach($children as $child) {
  40.         $iconMediaId $child->getCategory()->getCustomFields()['wyn_category_additional_information_icon'] ?? null;
  41.         if($iconMediaId != null) {
  42.           $categoryIconMediaIds[] = $iconMediaId;
  43.         }
  44.       }
  45.     }
  46.     $criteria = new Criteria();
  47.     $criteria->addFilter(new EqualsAnyFilter('id'$categoryIconMediaIds));
  48.     /** @var MediaCollection $result */
  49.     $mediaCollection $this->mediaRepository->search($criteria$event->getContext())->getEntities();
  50. //    $event->getNavigation()->addExtension('categoryIconMediaCollection', new ArrayStruct(['mediaCollection' => $mediaCollection]));
  51.     $struct = new ArrayStruct(['mediaCollection' => $mediaCollection]);
  52.     $event->getNavigation()->addExtension('categoryIconMediaCollection'$struct);
  53.     $hallo 5;
  54.   }
  55. }