/
NavigationPageLoader.php
97 lines (78 loc) · 3.76 KB
/
NavigationPageLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types=1);
namespace Shopware\Storefront\Page\Navigation;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Do not use direct or indirect repository calls in a PageLoader. Always use a store-api route to get or put data.
*/
#[Package('storefront')]
class NavigationPageLoader implements NavigationPageLoaderInterface
{
/**
* @internal
*/
public function __construct(
private readonly GenericPageLoaderInterface $genericLoader,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly AbstractCategoryRoute $cmsPageRoute,
private readonly SeoUrlPlaceholderHandlerInterface $seoUrlReplacer
) {
}
public function load(Request $request, SalesChannelContext $context): NavigationPage
{
$page = $this->genericLoader->load($request, $context);
$page = NavigationPage::createFrom($page);
$navigationId = $request->get('navigationId', $context->getSalesChannel()->getNavigationCategoryId());
$category = $this->cmsPageRoute
->load($navigationId, $request, $context)
->getCategory();
if (!$category->getActive()) {
throw new CategoryNotFoundException($category->getId());
}
$this->loadMetaData($category, $page, $context->getSalesChannel());
$page->setNavigationId($category->getId());
if ($category->getCmsPage()) {
$page->setCmsPage($category->getCmsPage());
}
$this->eventDispatcher->dispatch(
new NavigationPageLoadedEvent($page, $context, $request)
);
if ($page->getMetaInformation()) {
$canonical = ($navigationId === $context->getSalesChannel()->getNavigationCategoryId())
? $this->seoUrlReplacer->generate('frontend.home.page')
: $this->seoUrlReplacer->generate('frontend.navigation.page', ['navigationId' => $navigationId]);
$page->getMetaInformation()->setCanonical($canonical);
}
return $page;
}
private function loadMetaData(CategoryEntity $category, NavigationPage $page, SalesChannelEntity $salesChannel): void
{
$metaInformation = $page->getMetaInformation();
if ($metaInformation === null) {
return;
}
$isHome = $salesChannel->getNavigationCategoryId() === $category->getId();
$metaDescription = $isHome && $salesChannel->getTranslation('homeMetaDescription')
? $salesChannel->getTranslation('homeMetaDescription')
: $category->getTranslation('metaDescription')
?? $category->getTranslation('description');
$metaInformation->setMetaDescription((string) $metaDescription);
$metaTitle = $isHome && $salesChannel->getTranslation('homeMetaTitle')
? $salesChannel->getTranslation('homeMetaTitle')
: $category->getTranslation('metaTitle')
?? $category->getTranslation('name');
$metaInformation->setMetaTitle((string) $metaTitle);
$keywords = $isHome && $salesChannel->getTranslation('homeKeywords')
? $salesChannel->getTranslation('homeKeywords')
: $category->getTranslation('keywords');
$metaInformation->setMetaKeywords((string) $keywords);
}
}