fix sitemap: navigation with several domains

This commit is contained in:
Simon Vieille 2022-05-08 16:43:47 +02:00
parent e3f6793ce6
commit edc9c25d70
Signed by: deblan
GPG key ID: 579388D585F70417
3 changed files with 48 additions and 10 deletions

View file

@ -14,19 +14,20 @@ class SitemapController extends AbstractController
/** /**
* @Route("/sitemap.xml", name="sitemap") * @Route("/sitemap.xml", name="sitemap")
*/ */
public function sitemap(Request $request, NavigationRepositoryQuery $navigationRepositoryQuery, SitemapBuilder $builder): Response public function sitemap(Request $request, NavigationRepositoryQuery $query, SitemapBuilder $builder): Response
{ {
$navigations = $navigationRepositoryQuery $navigations = $query->create()->find();
->whereDomain($request->getHost())
->find()
;
$items = []; $items = [];
foreach ($navigations as $navigation) { foreach ($navigations as $navigation) {
if (!$navigation->matchDomain($request->getHost())) {
continue;
}
$items = array_merge( $items = array_merge(
$items, $items,
$builder->build($navigation) $builder->build($navigation, $request->getHost())
); );
} }

View file

@ -240,4 +240,23 @@ class Navigation implements EntityInterface
return $this; return $this;
} }
public function matchDomain(string $domain): bool
{
if ($domain === $this->getDomain()) {
return true;
}
foreach ($this->getAdditionalDomains() as $additionalDomain) {
if ($additionalDomain['type'] === 'domain' && $additionalDomain['domain'] === $domain) {
return true;
}
if ($additionalDomain['type'] === 'regexp' && preg_match('#'.$additionalDomain['domain'].'#', $domain) > 0) {
return true;
}
}
return false;
}
} }

View file

@ -28,7 +28,7 @@ class SitemapBuilder
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
} }
public function build(Navigation $navigation): array public function build(Navigation $navigation, ?string $currentDomain): array
{ {
$items = []; $items = [];
@ -52,7 +52,7 @@ class SitemapBuilder
$nodeItems = []; $nodeItems = [];
foreach ($this->getNodeUrls($node) as $url) { foreach ($this->getNodeUrls($node, $currentDomain) as $url) {
$nodeItems[] = $this->createItem($parameters, $url); $nodeItems[] = $this->createItem($parameters, $url);
} }
@ -66,7 +66,7 @@ class SitemapBuilder
return $items; return $items;
} }
public function getNodeUrls(Node $node) public function getNodeUrls(Node $node, ?string $currentDomain)
{ {
$urls = []; $urls = [];
@ -90,9 +90,27 @@ class SitemapBuilder
} }
} }
} elseif (!$node->getDisableUrl() && !$node->hasAppUrl()) { } elseif (!$node->getDisableUrl() && !$node->hasAppUrl()) {
$params = [];
$domain = $currentDomain;
$navigation = $node->getMenu()->getNavigation();
if (null === $currentDomain || $navigation->getForceDomain()) {
$domain = $navigation->getDomain();
}
$params['_domain'] = $domain;
foreach ($node->getParameters() as $param) {
if ('_locale' === $param['name']) {
$params['_locale'] = !empty($param['defaultValue'])
? $param['defaultValue']
: $node->getMenu()->getNavigation()->getLocale();
}
}
$urls[] = $this->urlGenerator->generate( $urls[] = $this->urlGenerator->generate(
$node->getRouteName(), $node->getRouteName(),
[], $params,
UrlGeneratorInterface::ABSOLUTE_URL UrlGeneratorInterface::ABSOLUTE_URL
); );
} }