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

Open in your IDE?
  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\Annotation\Concept\ExtensionPattern\Decoratable;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  11. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15.  * @Decoratable()
  16.  */
  17. #[Package('storefront')]
  18. class NavigationPageLoader implements NavigationPageLoaderInterface
  19. {
  20.     /**
  21.      * @var GenericPageLoaderInterface
  22.      */
  23.     private $genericLoader;
  24.     /**
  25.      * @var EventDispatcherInterface
  26.      */
  27.     private $eventDispatcher;
  28.     /**
  29.      * @var AbstractCategoryRoute
  30.      */
  31.     private $cmsPageRoute;
  32.     /**
  33.      * @var SeoUrlPlaceholderHandlerInterface
  34.      */
  35.     private $seoUrlReplacer;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         GenericPageLoaderInterface $genericLoader,
  41.         EventDispatcherInterface $eventDispatcher,
  42.         AbstractCategoryRoute $cmsPageRoute,
  43.         SeoUrlPlaceholderHandlerInterface $seoUrlReplacer
  44.     ) {
  45.         $this->genericLoader $genericLoader;
  46.         $this->eventDispatcher $eventDispatcher;
  47.         $this->cmsPageRoute $cmsPageRoute;
  48.         $this->seoUrlReplacer $seoUrlReplacer;
  49.     }
  50.     public function load(Request $requestSalesChannelContext $context): NavigationPage
  51.     {
  52.         $page $this->genericLoader->load($request$context);
  53.         $page NavigationPage::createFrom($page);
  54.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  55.         $category $this->cmsPageRoute
  56.             ->load($navigationId$request$context)
  57.             ->getCategory();
  58.         if (!$category->getActive()) {
  59.             throw new CategoryNotFoundException($category->getId());
  60.         }
  61.         $this->loadMetaData($category$page$context->getSalesChannel());
  62.         $page->setNavigationId($category->getId());
  63.         if ($category->getCmsPage()) {
  64.             $page->setCmsPage($category->getCmsPage());
  65.         }
  66.         $this->eventDispatcher->dispatch(
  67.             new NavigationPageLoadedEvent($page$context$request)
  68.         );
  69.         if ($page->getMetaInformation()) {
  70.             $canonical = ($navigationId === $context->getSalesChannel()->getNavigationCategoryId())
  71.                 ? $this->seoUrlReplacer->generate('frontend.home.page')
  72.                 : $this->seoUrlReplacer->generate('frontend.navigation.page', ['navigationId' => $navigationId]);
  73.             $page->getMetaInformation()->setCanonical($canonical);
  74.         }
  75.         return $page;
  76.     }
  77.     private function loadMetaData(CategoryEntity $categoryNavigationPage $pageSalesChannelEntity $salesChannel): void
  78.     {
  79.         $metaInformation $page->getMetaInformation();
  80.         if ($metaInformation === null) {
  81.             return;
  82.         }
  83.         $isHome $salesChannel->getNavigationCategoryId() === $category->getId();
  84.         $metaDescription $isHome && $salesChannel->getTranslation('homeMetaDescription')
  85.             ? $salesChannel->getTranslation('homeMetaDescription')
  86.             : $category->getTranslation('metaDescription')
  87.             ?? $category->getTranslation('description');
  88.         $metaInformation->setMetaDescription((string) $metaDescription);
  89.         $metaTitle $isHome && $salesChannel->getTranslation('homeMetaTitle')
  90.             ? $salesChannel->getTranslation('homeMetaTitle')
  91.             : $category->getTranslation('metaTitle')
  92.             ?? $category->getTranslation('name');
  93.         $metaInformation->setMetaTitle((string) $metaTitle);
  94.         $keywords $isHome && $salesChannel->getTranslation('homeKeywords')
  95.             ? $salesChannel->getTranslation('homeKeywords')
  96.             : $category->getTranslation('keywords');
  97.         $metaInformation->setMetaKeywords((string) $keywords);
  98.     }
  99. }