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

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