Skip to content

Commit

Permalink
Merge pull request #40 from stefandoorn/static-content-locales
Browse files Browse the repository at this point in the history
Fix duplicate results in static content sitemap (fixes #39)
  • Loading branch information
stefandoorn authored Feb 27, 2018
2 parents cde1a64 + ca49475 commit d59d50c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function addSitemapSection(ArrayNodeDefinition $node): void
->end()
->arrayNode('locales')
->prototype('scalar')
->info('Define which locales to add. If empty, it uses the default locale context supplied')
->info('Define which locales to add. If empty, it uses the default locales for channel context supplied')
->end()
->end()
->end()
Expand Down
33 changes: 28 additions & 5 deletions src/Provider/StaticUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,21 @@ public function generate(): iterable
$staticUrl->setChangeFrequency(ChangeFrequency::weekly());
$staticUrl->setPriority(0.3);

// Populate locales array by other enabled locales for current channel if no locales are specified
if (!isset($route['locales']) || empty($route['locales'])) {
$route['locales'] = $this->getAlternativeLocales($channel);
}

// Add default locale to route if not set
if (!array_key_exists('_locale', $route['parameters'])) {
if ($channel->getDefaultLocale()) {
$route['parameters']['_locale'] = $channel->getDefaultLocale()->getCode();
}
}

// Populate locales array by other enabled locales for current channel if no locales are specified
if (!isset($route['locales']) || empty($route['locales'])) {
$route['locales'] = $this->getAlternativeLocales($channel);
}

// Remove the locale that is on the main route from the alternatives to prevent duplicates
$route = $this->excludeMainRouteLocaleFromAlternativeLocales($route);

$location = $this->router->generate($route['route'], $route['parameters']);
$staticUrl->setLocalization($location);

Expand All @@ -107,6 +112,24 @@ public function generate(): iterable
return $this->urls;
}

/**
* @param array $route
* @return array
*/
private function excludeMainRouteLocaleFromAlternativeLocales(array $route): array
{
$locales = $route['locales'];
$locale = $route['parameters']['_locale'];

$key = array_search($locale, $locales);

if ($key !== false) {
unset($route['locales'][$key]);
}

return $route;
}

/**
* @param ChannelInterface $channel
*
Expand Down
1 change: 1 addition & 0 deletions tests/Application/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ imports:
sitemap:
static_routes:
- { route: sylius_shop_order_show, parameters: { tokenValue: fooToken } }
- { route: sylius_shop_login, locales: [nl_NL, en_US] }

framework:
translator: { fallbacks: ["%locale%"] }
Expand Down
14 changes: 14 additions & 0 deletions tests/Responses/Expected/show_sitemap_all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/en_US/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
</urlset>
14 changes: 14 additions & 0 deletions tests/Responses/Expected/show_sitemap_all_relative.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>/en_US/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>/nl_NL/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
</urlset>
14 changes: 14 additions & 0 deletions tests/Responses/Expected/show_sitemap_static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/en_US/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>http://localhost/nl_NL/login</loc>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost/en_US/login"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost/nl_NL/login"/>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
</urlset>

0 comments on commit d59d50c

Please sign in to comment.