custom/static-plugins/EfbStorefront/Subscriber/HeaderSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\EFB\Storefront\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\Tree\Tree;
  5. use Shopware\Core\Content\Category\Tree\TreeItem;
  6. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Event\NestedEvent;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Event\StorefrontRenderEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  14. use Shopware\Storefront\Page\PageLoadedEvent;
  15. use Shopware\Storefront\Pagelet\Header\HeaderPagelet;
  16. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class HeaderSubscriber implements EventSubscriberInterface
  19. {
  20.     private const CATEGORY_FIELD_NAMES = [
  21.         'custom_fields_sales_channel_cat_downloads',
  22.         'custom_fields_sales_channel_cat_contact',
  23.     ];
  24.     private EntityRepository $categoryRepository;
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  29.         ];
  30.     }
  31.     public function __construct(EntityRepository $categoryRepository)
  32.     {
  33.         $this->categoryRepository $categoryRepository;
  34.     }
  35.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event) {
  36.         $this->fetchTopBarElements($event->getSalesChannelContext(), $event->getPagelet());
  37.         $this->correctActive($event->getPagelet()->getNavigation(), $event->getSalesChannelContext());
  38.     }
  39.     private function fetchTopBarElements(SalesChannelContext $contextHeaderPagelet $pagelet) {
  40.         $salesChannelCustomFields $context->getSalesChannel()->getTranslated()['customFields'];
  41.         $ids = [];
  42.         foreach (self::CATEGORY_FIELD_NAMES as $index => $fieldName) {
  43.             if (array_key_exists($fieldName$salesChannelCustomFields)) {
  44.                 $ids[$index] = $salesChannelCustomFields[$fieldName];
  45.             }
  46.         }
  47.         $categories $this->categoryRepository->search(
  48.             new Criteria($ids),
  49.             $context->getContext()
  50.         );
  51.         if ($categories->count() !== count($ids)) {
  52.             $ids $categories->getIds();
  53.         }
  54.         $pagelet->topbarElements = [
  55.             $categories->get(array_shift($ids)),
  56.             'language-widget',
  57.             $categories->get(array_shift($ids)),
  58.         ];
  59.     }
  60.     private function correctActive(Tree $navigationSalesChannelContext $context)
  61.     {
  62.         $salesChannel $context->getSalesChannel();
  63.         $activePath $navigation->getActive()->getPath();
  64.         if (null === $activePath) {
  65.             return;
  66.         }
  67.         $activePath array_filter(explode('|'$activePath));
  68.         if (count($activePath) < $salesChannel->getNavigationCategoryDepth()) {
  69.             return;
  70.         }
  71.         $activePath array_slice($activePath1$salesChannel->getNavigationCategoryDepth());
  72.         foreach (array_reverse($activePath) as $category) {
  73.             $result $this->findInTree($category$navigation->getTree());
  74.             if (null === $result) {
  75.                 continue;
  76.             }
  77.             $navigation->setActive($result->getCategory());
  78.             return;
  79.         }
  80.         $navigation->setActive($salesChannel->getNavigationCategory());
  81.     }
  82.     /**
  83.      * @param TreeItem[] $tree
  84.      */
  85.     private function findInTree(string $categoryId, array $tree): ?TreeItem
  86.     {
  87.         if (isset($tree[$categoryId])) {
  88.             return $tree[$categoryId];
  89.         }
  90.         foreach ($tree as $item) {
  91.             $nested $this->findInTree($categoryId$item->getChildren());
  92.             if ($nested) {
  93.                 return $nested;
  94.             }
  95.         }
  96.         return null;
  97.     }
  98. }