<?php declare(strict_types=1);
namespace Tonur\SeoFilterLandingpages\Storefront\Page\Landingpage;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Shopware\Storefront\Page\Navigation\NavigationPage;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Tonur\SeoFilterLandingpages\Core\Content\Landingpage\SalesChannel\AbstractLandingpageRoute;
use Tonur\SeoFilterLandingpages\Core\Content\Landingpage\SeoFilterLandingpageEntity;
use Tonur\SeoFilterLandingpages\Storefront\Seo\LandingpageSeoUrlRoute;
class LandingpagePageLoader
{
/**
* @var GenericPageLoaderInterface
*/
private $genericLoader;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var AbstractLandingpageRoute
*/
private $cmsPageRoute;
/**
* @var SeoUrlPlaceholderHandlerInterface
*/
private $seoUrlPlaceholderHandler;
public function __construct(
GenericPageLoaderInterface $genericLoader,
EventDispatcherInterface $eventDispatcher,
AbstractLandingpageRoute $cmsPageRoute,
SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler
) {
$this->genericLoader = $genericLoader;
$this->eventDispatcher = $eventDispatcher;
$this->cmsPageRoute = $cmsPageRoute;
$this->seoUrlPlaceholderHandler = $seoUrlPlaceholderHandler;
}
public function load(Request $request, SalesChannelContext $context): LandingpagePage
{
$landingpageId = $request->get('landingpageId');
$landingpage = $this->cmsPageRoute
->load($landingpageId, $request, $context)
->getLandingpage();
if ($landingpage && $landingpage->getCategoryId()) {
$request->request->set('navigationId', $landingpage->getCategoryId());
}
$page = $this->genericLoader->load($request, $context);
$page = LandingpagePage::createFrom($page);
$page->setLandingpage($landingpage);
$host = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
. $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
$url = $this->seoUrlPlaceholderHandler->replace(
$this->seoUrlPlaceholderHandler->generate(
'frontend.navigation.page',
['navigationId' => $landingpage->getCategoryId()]
),
$host,
$context
);
$page->setParentCategoryUrl($url);
if ($landingpage->getCmsPage()) {
$this->loadMetaData($landingpage, $page);
$page->setCmsPage($landingpage->getCmsPage());
$page->setNavigationId($landingpage->getCategoryId());
}
$this->eventDispatcher->dispatch(
new LandingpagePageLoadedEvent($page, $context, $request)
);
return $page;
}
private function loadMetaData(SeoFilterLandingpageEntity $landingpage, NavigationPage $page): void
{
$metaInformation = $page->getMetaInformation();
$robots = $landingpage->getRobotType() ?? $metaInformation->getRobots();
$metaInformation->setRobots($robots);
$metaDescription = $landingpage->getTranslation('metaDescription')
?? $landingpage->getTranslation('description');
$metaInformation->setMetaDescription((string) $metaDescription);
$metaTitle = $landingpage->getTranslation('metaTitle')
?? $landingpage->getTranslation('name');
$metaInformation->setMetaTitle((string) $metaTitle);
$metaInformation->setMetaKeywords((string) $landingpage->getTranslation('keywords'));
$landingpageUrl = $this->seoUrlPlaceholderHandler->generate(
LandingpageSeoUrlRoute::ROUTE_NAME ,
['landingpageId' => $landingpage->getId()],
);
$metaInformation->assign(['canonical' => $landingpageUrl]);
}
}