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

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.     public function __construct(
  35.         GenericPageLoaderInterface $genericLoader,
  36.         EventDispatcherInterface $eventDispatcher,
  37.         AbstractCategoryRoute $cmsPageRoute,
  38.         SeoUrlPlaceholderHandlerInterface $seoUrlReplacer
  39.     ) {
  40.         $this->genericLoader $genericLoader;
  41.         $this->eventDispatcher $eventDispatcher;
  42.         $this->cmsPageRoute $cmsPageRoute;
  43.         $this->seoUrlReplacer $seoUrlReplacer;
  44.     }
  45.     public function load(Request $requestSalesChannelContext $context): NavigationPage
  46.     {
  47.         $page $this->genericLoader->load($request$context);
  48.         $page NavigationPage::createFrom($page);
  49.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  50.         $category $this->cmsPageRoute
  51.             ->load($navigationId$request$context)
  52.             ->getCategory();
  53.         if (!$category->getActive()) {
  54.             throw new CategoryNotFoundException($category->getId());
  55.         }
  56.         if ($category->getCmsPage()) {
  57.             $this->loadMetaData($category$page$context->getSalesChannel());
  58.             $page->setCmsPage($category->getCmsPage());
  59.             $page->setNavigationId($category->getId());
  60.         }
  61.         $this->eventDispatcher->dispatch(
  62.             new NavigationPageLoadedEvent($page$context$request)
  63.         );
  64.         if ($page->getMetaInformation()) {
  65.             $canonical = ($navigationId === $context->getSalesChannel()->getNavigationCategoryId())
  66.                 ? $this->seoUrlReplacer->generate('frontend.home.page')
  67.                 : $this->seoUrlReplacer->generate('frontend.navigation.page', ['navigationId' => $navigationId]);
  68.             $page->getMetaInformation()->assign(['canonical' => $canonical]);
  69.         }
  70.         return $page;
  71.     }
  72.     private function loadMetaData(CategoryEntity $categoryNavigationPage $pageSalesChannelEntity $salesChannel): void
  73.     {
  74.         $metaInformation $page->getMetaInformation();
  75.         if ($metaInformation === null) {
  76.             return;
  77.         }
  78.         $isHome $salesChannel->getNavigationCategoryId() === $category->getId();
  79.         $metaDescription $isHome && $salesChannel->getTranslation('homeMetaDescription')
  80.             ? $salesChannel->getTranslation('homeMetaDescription')
  81.             : $category->getTranslation('metaDescription')
  82.             ?? $category->getTranslation('description');
  83.         $metaInformation->setMetaDescription((string) $metaDescription);
  84.         $metaTitle $isHome && $salesChannel->getTranslation('homeMetaTitle')
  85.             ? $salesChannel->getTranslation('homeMetaTitle')
  86.             : $category->getTranslation('metaTitle')
  87.             ?? $category->getTranslation('name');
  88.         $metaInformation->setMetaTitle((string) $metaTitle);
  89.         $keywords $isHome && $salesChannel->getTranslation('homeKeywords')
  90.             ? $salesChannel->getTranslation('homeKeywords')
  91.             : $category->getTranslation('keywords');
  92.         $metaInformation->setMetaKeywords((string) $keywords);
  93.     }
  94. }