custom/static-plugins/LnbThemeDefault/src/Subscriber/HeaderPageletLoadedSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. /**
  3.  * gpl-3.0 license
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  17.  *
  18.  * @package    shop.liebscher-bracht.com
  19.  * @author     Michael Lämmlein <michael.laemmlein@liebscher-bracht.com>
  20.  * @copyright  ©2024 Liebscher & Bracht
  21.  * @license    https://www.gnu.org/licenses/gpl-3.0.html
  22.  * @version    1.0.0
  23.  * @since      18.09.24
  24.  */
  25. declare(strict_types=1);
  26. namespace Lnb\Shopware6\LnbThemeDefault\Subscriber;
  27. use Shopware\Core\Content\Category\Tree\TreeItem;
  28. use Shopware\Core\Content\Media\MediaCollection;
  29. use Shopware\Core\Framework\Context;
  30. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  31. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  32. use Shopware\Core\Framework\Struct\ArrayStruct;
  33. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  34. use Symfony\Component\DependencyInjection\Attribute\Target;
  35. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  36. class HeaderPageletLoadedSubscriber implements EventSubscriberInterface
  37. {
  38.     public function __construct(
  39.         #[Target('media.repository')] private readonly EntityRepository $mediaRepository
  40.     ) {
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  46.         ];
  47.     }
  48.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event)
  49.     {
  50.         $ids $this->collectIds($event->getPagelet()->getNavigation()->getTree(), []);
  51.         $mediaCollection $this->searchMedia($ids$event->getContext());
  52.         $event->getPagelet()->addExtension('lnb_theme_default_category_navigation_icons', new ArrayStruct([
  53.             'mediaCollection' => $mediaCollection
  54.         ]));
  55.     }
  56.     private function collectIds(array $tree, array $ids): array
  57.     {
  58.         /** @var TreeItem $item */
  59.         foreach ($tree as $item) {
  60.             if ($item->getChildren() > 0) {
  61.                 $ids $this->collectIds($item->getChildren(), $ids);
  62.             }
  63.             $customFields $item->getCategory()->getCustomFields();
  64.             if (!isset($customFields['custom_configurations_category_navigation_icon'])
  65.                 || !$customFields['custom_configurations_category_navigation_icon']
  66.             ) {
  67.                 continue;
  68.             }
  69.             $ids[] = $customFields['custom_configurations_category_navigation_icon'];
  70.         }
  71.         return $ids;
  72.     }
  73.     private function searchMedia(array $idsContext $context): MediaCollection
  74.     {
  75.         if (empty($ids)) {
  76.             return new MediaCollection();
  77.         }
  78.         $criteria = new Criteria($ids);
  79.         /** @var MediaCollection $media */
  80.         $media $this->mediaRepository
  81.             ->search($criteria$context)
  82.             ->getEntities();
  83.         return $media;
  84.     }
  85. }