src/Storefront/Page/Navigation/NavigationPageLoader.php line 34

  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. #[Package('storefront')]
  14. class NavigationPageLoader implements NavigationPageLoaderInterface
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(private readonly GenericPageLoaderInterface $genericLoader, private readonly EventDispatcherInterface $eventDispatcher, private readonly AbstractCategoryRoute $cmsPageRoute, private readonly SeoUrlPlaceholderHandlerInterface $seoUrlReplacer)
  20.     {
  21.     }
  22.     public function load(Request $requestSalesChannelContext $context): NavigationPage
  23.     {
  24.         $page $this->genericLoader->load($request$context);
  25.         $page NavigationPage::createFrom($page);
  26.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  27.         $category $this->cmsPageRoute
  28.             ->load($navigationId$request$context)
  29.             ->getCategory();
  30.         if (!$category->getActive()) {
  31.             throw new CategoryNotFoundException($category->getId());
  32.         }
  33.         $this->loadMetaData($category$page$context->getSalesChannel());
  34.         $page->setNavigationId($category->getId());
  35.         if ($category->getCmsPage()) {
  36.             $page->setCmsPage($category->getCmsPage());
  37.         }
  38.         $this->eventDispatcher->dispatch(
  39.             new NavigationPageLoadedEvent($page$context$request)
  40.         );
  41.         if ($page->getMetaInformation()) {
  42.             $canonical = ($navigationId === $context->getSalesChannel()->getNavigationCategoryId())
  43.                 ? $this->seoUrlReplacer->generate('frontend.home.page')
  44.                 : $this->seoUrlReplacer->generate('frontend.navigation.page', ['navigationId' => $navigationId]);
  45.             $page->getMetaInformation()->setCanonical($canonical);
  46.         }
  47.         return $page;
  48.     }
  49.     private function loadMetaData(CategoryEntity $categoryNavigationPage $pageSalesChannelEntity $salesChannel): void
  50.     {
  51.         $metaInformation $page->getMetaInformation();
  52.         if ($metaInformation === null) {
  53.             return;
  54.         }
  55.         $isHome $salesChannel->getNavigationCategoryId() === $category->getId();
  56.         $metaDescription $isHome && $salesChannel->getTranslation('homeMetaDescription')
  57.             ? $salesChannel->getTranslation('homeMetaDescription')
  58.             : $category->getTranslation('metaDescription')
  59.             ?? $category->getTranslation('description');
  60.         $metaInformation->setMetaDescription((string) $metaDescription);
  61.         $metaTitle $isHome && $salesChannel->getTranslation('homeMetaTitle')
  62.             ? $salesChannel->getTranslation('homeMetaTitle')
  63.             : $category->getTranslation('metaTitle')
  64.             ?? $category->getTranslation('name');
  65.         $metaInformation->setMetaTitle((string) $metaTitle);
  66.         $keywords $isHome && $salesChannel->getTranslation('homeKeywords')
  67.             ? $salesChannel->getTranslation('homeKeywords')
  68.             : $category->getTranslation('keywords');
  69.         $metaInformation->setMetaKeywords((string) $keywords);
  70.     }
  71. }