custom/plugins/TonurSeoFilterLandingpages6/src/Storefront/Page/Landingpage/LandingpagePageLoader.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tonur\SeoFilterLandingpages\Storefront\Page\Landingpage;
  3. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  6. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  7. use Shopware\Storefront\Page\Navigation\NavigationPage;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Tonur\SeoFilterLandingpages\Core\Content\Landingpage\SalesChannel\AbstractLandingpageRoute;
  11. use Tonur\SeoFilterLandingpages\Core\Content\Landingpage\SeoFilterLandingpageEntity;
  12. use Tonur\SeoFilterLandingpages\Storefront\Seo\LandingpageSeoUrlRoute;
  13. class LandingpagePageLoader
  14. {
  15.     /**
  16.      * @var GenericPageLoaderInterface
  17.      */
  18.     private $genericLoader;
  19.     /**
  20.      * @var EventDispatcherInterface
  21.      */
  22.     private $eventDispatcher;
  23.     /**
  24.      * @var AbstractLandingpageRoute
  25.      */
  26.     private $cmsPageRoute;
  27.     /**
  28.      * @var SeoUrlPlaceholderHandlerInterface
  29.      */
  30.     private $seoUrlPlaceholderHandler;
  31.     public function __construct(
  32.         GenericPageLoaderInterface $genericLoader,
  33.         EventDispatcherInterface $eventDispatcher,
  34.         AbstractLandingpageRoute $cmsPageRoute,
  35.         SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler
  36.     ) {
  37.         $this->genericLoader $genericLoader;
  38.         $this->eventDispatcher $eventDispatcher;
  39.         $this->cmsPageRoute $cmsPageRoute;
  40.         $this->seoUrlPlaceholderHandler $seoUrlPlaceholderHandler;
  41.     }
  42.     public function load(Request $requestSalesChannelContext $context): LandingpagePage
  43.     {
  44.         $landingpageId $request->get('landingpageId');
  45.         $landingpage $this->cmsPageRoute
  46.             ->load($landingpageId$request$context)
  47.             ->getLandingpage();
  48.         if ($landingpage && $landingpage->getCategoryId()) {
  49.             $request->request->set('navigationId'$landingpage->getCategoryId());
  50.         }
  51.         $page $this->genericLoader->load($request$context);
  52.         $page LandingpagePage::createFrom($page);
  53.         $page->setLandingpage($landingpage);
  54.         $host $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
  55.             . $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  56.         $url $this->seoUrlPlaceholderHandler->replace(
  57.             $this->seoUrlPlaceholderHandler->generate(
  58.                 'frontend.navigation.page',
  59.                 ['navigationId' => $landingpage->getCategoryId()]
  60.             ),
  61.             $host,
  62.             $context
  63.         );
  64.         $page->setParentCategoryUrl($url);
  65.         if ($landingpage->getCmsPage()) {
  66.             $this->loadMetaData($landingpage$page);
  67.             $page->setCmsPage($landingpage->getCmsPage());
  68.             $page->setNavigationId($landingpage->getCategoryId());
  69.         }
  70.         $this->eventDispatcher->dispatch(
  71.             new LandingpagePageLoadedEvent($page$context$request)
  72.         );
  73.         return $page;
  74.     }
  75.     private function loadMetaData(SeoFilterLandingpageEntity $landingpageNavigationPage $page): void
  76.     {
  77.         $metaInformation $page->getMetaInformation();
  78.         $robots $landingpage->getRobotType() ?? $metaInformation->getRobots();
  79.         $metaInformation->setRobots($robots);
  80.         $metaDescription $landingpage->getTranslation('metaDescription')
  81.             ?? $landingpage->getTranslation('description');
  82.         $metaInformation->setMetaDescription((string) $metaDescription);
  83.         $metaTitle $landingpage->getTranslation('metaTitle')
  84.             ?? $landingpage->getTranslation('name');
  85.         $metaInformation->setMetaTitle((string) $metaTitle);
  86.         $metaInformation->setMetaKeywords((string) $landingpage->getTranslation('keywords'));
  87.         $landingpageUrl $this->seoUrlPlaceholderHandler->generate(
  88.             LandingpageSeoUrlRoute::ROUTE_NAME ,
  89.             ['landingpageId' => $landingpage->getId()],
  90.         );
  91.         $metaInformation->assign(['canonical' => $landingpageUrl]);
  92.     }
  93. }