vendor/shopware/storefront/Page/Navigation/NavigationPageLoader.php line 35

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Navigation;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  5. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  6. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  10. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. /**
  14.  * Do not use direct or indirect repository calls in a PageLoader. Always use a store-api route to get or put data.
  15.  */
  16. #[Package('storefront')]
  17. class NavigationPageLoader implements NavigationPageLoaderInterface
  18. {
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         private readonly GenericPageLoaderInterface $genericLoader,
  24.         private readonly EventDispatcherInterface $eventDispatcher,
  25.         private readonly AbstractCategoryRoute $cmsPageRoute,
  26.         private readonly SeoUrlPlaceholderHandlerInterface $seoUrlReplacer
  27.     ) {
  28.     }
  29.     public function load(Request $requestSalesChannelContext $context): NavigationPage
  30.     {
  31.         $page $this->genericLoader->load($request$context);
  32.         $page NavigationPage::createFrom($page);
  33.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  34.         $category $this->cmsPageRoute
  35.             ->load($navigationId$request$context)
  36.             ->getCategory();
  37.         if (!$category->getActive()) {
  38.             throw new CategoryNotFoundException($category->getId());
  39.         }
  40.         $this->loadMetaData($category$page$context->getSalesChannel());
  41.         $page->setNavigationId($category->getId());
  42.         if ($category->getCmsPage()) {
  43.             $page->setCmsPage($category->getCmsPage());
  44.         }
  45.         $this->eventDispatcher->dispatch(
  46.             new NavigationPageLoadedEvent($page$context$request)
  47.         );
  48.         if ($page->getMetaInformation()) {
  49.             $canonical = ($navigationId === $context->getSalesChannel()->getNavigationCategoryId())
  50.                 ? $this->seoUrlReplacer->generate('frontend.home.page')
  51.                 : $this->seoUrlReplacer->generate('frontend.navigation.page', ['navigationId' => $navigationId]);
  52.             $page->getMetaInformation()->setCanonical($canonical);
  53.         }
  54.         return $page;
  55.     }
  56.     private function loadMetaData(CategoryEntity $categoryNavigationPage $pageSalesChannelEntity $salesChannel): void
  57.     {
  58.         $metaInformation $page->getMetaInformation();
  59.         if ($metaInformation === null) {
  60.             return;
  61.         }
  62.         $isHome $salesChannel->getNavigationCategoryId() === $category->getId();
  63.         $metaDescription $isHome && $salesChannel->getTranslation('homeMetaDescription')
  64.             ? $salesChannel->getTranslation('homeMetaDescription')
  65.             : $category->getTranslation('metaDescription')
  66.             ?? $category->getTranslation('description');
  67.         $metaInformation->setMetaDescription((string) $metaDescription);
  68.         $metaTitle $isHome && $salesChannel->getTranslation('homeMetaTitle')
  69.             ? $salesChannel->getTranslation('homeMetaTitle')
  70.             : $category->getTranslation('metaTitle')
  71.             ?? $category->getTranslation('name');
  72.         $metaInformation->setMetaTitle((string) $metaTitle);
  73.         $keywords $isHome && $salesChannel->getTranslation('homeKeywords')
  74.             ? $salesChannel->getTranslation('homeKeywords')
  75.             : $category->getTranslation('keywords');
  76.         $metaInformation->setMetaKeywords((string) $keywords);
  77.     }
  78. }