vendor/shopware/storefront/Pagelet/Header/HeaderPageletLoader.php line 51

  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. /**
  20.  * Do not use direct or indirect repository calls in a PageletLoader. Always use a store-api route to get or put data.
  21.  */
  22. #[Package('storefront')]
  23. class HeaderPageletLoader implements HeaderPageletLoaderInterface
  24. {
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(
  29.         private readonly EventDispatcherInterface $eventDispatcher,
  30.         private readonly AbstractCurrencyRoute $currencyRoute,
  31.         private readonly AbstractLanguageRoute $languageRoute,
  32.         private readonly NavigationLoaderInterface $navigationLoader
  33.     ) {
  34.     }
  35.     /**
  36.      * @throws MissingRequestParameterException
  37.      */
  38.     public function load(Request $requestSalesChannelContext $context): HeaderPagelet
  39.     {
  40.         $salesChannel $context->getSalesChannel();
  41.         $navigationId $request->get('navigationId'$salesChannel->getNavigationCategoryId());
  42.         if (!$navigationId) {
  43.             throw new MissingRequestParameterException('navigationId');
  44.         }
  45.         $languages $this->getLanguages($context$request);
  46.         $event = new CurrencyRouteRequestEvent($request, new Request(), $context);
  47.         $this->eventDispatcher->dispatch($event);
  48.         $navigation $this->navigationLoader->load(
  49.             (string) $navigationId,
  50.             $context,
  51.             $salesChannel->getNavigationCategoryId(),
  52.             $salesChannel->getNavigationCategoryDepth()
  53.         );
  54.         $criteria = new Criteria();
  55.         $criteria->setTitle('header::currencies');
  56.         $currencies $this->currencyRoute
  57.             ->load($event->getStoreApiRequest(), $context$criteria)
  58.             ->getCurrencies();
  59.         $contextLanguage $languages->get($context->getContext()->getLanguageId());
  60.         if (!$contextLanguage) {
  61.             throw new \RuntimeException(sprintf('Context language with id %s not found'$context->getContext()->getLanguageId()));
  62.         }
  63.         $page = new HeaderPagelet(
  64.             $navigation,
  65.             $languages,
  66.             $currencies,
  67.             $contextLanguage,
  68.             $context->getCurrency(),
  69.             $this->getServiceMenu($context)
  70.         );
  71.         $this->eventDispatcher->dispatch(new HeaderPageletLoadedEvent($page$context$request));
  72.         return $page;
  73.     }
  74.     private function getServiceMenu(SalesChannelContext $context): CategoryCollection
  75.     {
  76.         $serviceId $context->getSalesChannel()->getServiceCategoryId();
  77.         if ($serviceId === null) {
  78.             return new CategoryCollection();
  79.         }
  80.         $navigation $this->navigationLoader->load($serviceId$context$serviceId1);
  81.         return new CategoryCollection(array_map(static fn (TreeItem $treeItem) => $treeItem->getCategory(), $navigation->getTree()));
  82.     }
  83.     private function getLanguages(SalesChannelContext $contextRequest $request): LanguageCollection
  84.     {
  85.         $criteria = new Criteria();
  86.         $criteria->setTitle('header::languages');
  87.         $criteria->addFilter(
  88.             new EqualsFilter('language.salesChannelDomains.salesChannelId'$context->getSalesChannel()->getId())
  89.         );
  90.         $criteria->addSorting(new FieldSorting('name'FieldSorting::ASCENDING));
  91.         $criteria->addAssociation('productSearchConfig');
  92.         $apiRequest = new Request();
  93.         $event = new LanguageRouteRequestEvent($request$apiRequest$context$criteria);
  94.         $this->eventDispatcher->dispatch($event);
  95.         return $this->languageRoute->load($event->getStoreApiRequest(), $context$criteria)->getLanguages();
  96.     }
  97. }