src/Storefront/Pagelet/Header/HeaderPageletLoader.php line 73

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Pagelet\Header;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
  5. use Shopware\Core\Content\Category\Tree\TreeItem;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  11. use Shopware\Core\System\Currency\SalesChannel\AbstractCurrencyRoute;
  12. use Shopware\Core\System\Language\LanguageCollection;
  13. use Shopware\Core\System\Language\SalesChannel\AbstractLanguageRoute;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Event\RouteRequest\CurrencyRouteRequestEvent;
  16. use Shopware\Storefront\Event\RouteRequest\LanguageRouteRequestEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. #[Package('storefront')]
  20. class HeaderPageletLoader implements HeaderPageletLoaderInterface
  21. {
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(private readonly EventDispatcherInterface $eventDispatcher, private readonly AbstractCurrencyRoute $currencyRoute, private readonly AbstractLanguageRoute $languageRoute, private readonly NavigationLoaderInterface $navigationLoader)
  26.     {
  27.     }
  28.     /**
  29.      * @throws MissingRequestParameterException
  30.      */
  31.     public function load(Request $requestSalesChannelContext $context): HeaderPagelet
  32.     {
  33.         $salesChannel $context->getSalesChannel();
  34.         $navigationId $request->get('navigationId'$salesChannel->getNavigationCategoryId());
  35.         if (!$navigationId) {
  36.             throw new MissingRequestParameterException('navigationId');
  37.         }
  38.         $languages $this->getLanguages($context$request);
  39.         $event = new CurrencyRouteRequestEvent($request, new Request(), $context);
  40.         $this->eventDispatcher->dispatch($event);
  41.         $navigation $this->navigationLoader->load(
  42.             (string) $navigationId,
  43.             $context,
  44.             $salesChannel->getNavigationCategoryId(),
  45.             $salesChannel->getNavigationCategoryDepth()
  46.         );
  47.         $criteria = new Criteria();
  48.         $criteria->setTitle('header::currencies');
  49.         $currencies $this->currencyRoute
  50.             ->load($event->getStoreApiRequest(), $context$criteria)
  51.             ->getCurrencies();
  52.         $contextLanguage $languages->get($context->getContext()->getLanguageId());
  53.         if (!$contextLanguage) {
  54.             throw new \RuntimeException(sprintf('Context language with id %s not found'$context->getContext()->getLanguageId()));
  55.         }
  56.         $page = new HeaderPagelet(
  57.             $navigation,
  58.             $languages,
  59.             $currencies,
  60.             $contextLanguage,
  61.             $context->getCurrency(),
  62.             $this->getServiceMenu($context)
  63.         );
  64.         $this->eventDispatcher->dispatch(new HeaderPageletLoadedEvent($page$context$request));
  65.         return $page;
  66.     }
  67.     private function getServiceMenu(SalesChannelContext $context): CategoryCollection
  68.     {
  69.         $serviceId $context->getSalesChannel()->getServiceCategoryId();
  70.         if ($serviceId === null) {
  71.             return new CategoryCollection();
  72.         }
  73.         $navigation $this->navigationLoader->load($serviceId$context$serviceId1);
  74.         return new CategoryCollection(array_map(static fn (TreeItem $treeItem) => $treeItem->getCategory(), $navigation->getTree()));
  75.     }
  76.     private function getLanguages(SalesChannelContext $contextRequest $request): LanguageCollection
  77.     {
  78.         $criteria = new Criteria();
  79.         $criteria->setTitle('header::languages');
  80.         $criteria->addFilter(
  81.             new EqualsFilter('language.salesChannelDomains.salesChannelId'$context->getSalesChannel()->getId())
  82.         );
  83.         $criteria->addSorting(new FieldSorting('name'FieldSorting::ASCENDING));
  84.         $criteria->addAssociation('productSearchConfig');
  85.         $apiRequest = new Request();
  86.         $event = new LanguageRouteRequestEvent($request$apiRequest$context$criteria);
  87.         $this->eventDispatcher->dispatch($event);
  88.         return $this->languageRoute->load($event->getStoreApiRequest(), $context$criteria)->getLanguages();
  89.     }
  90. }