Skip to content

Commit

Permalink
Merge pull request #615 from tractorcow-farm/fix/x-default-tags
Browse files Browse the repository at this point in the history
BUG Properly handle x-default for home page tags
  • Loading branch information
tractorcow authored May 20, 2020
2 parents 8927892 + 381a351 commit 365b2cc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
39 changes: 34 additions & 5 deletions docs/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ displayed in the default locale.

`TractorCow\Fluent\Extension\FluentDirectorExtension.disable_default_prefix` (default: `false`)

This option will allow users to exclude the default locale urlsegment from pages in the default locale.

*Default behaviour (disabled)*

If this is left at the default, false, then the default locale will always be prefixed with the
urlsegment of that locale.

In addition to this, another hreflang will be generated for the root url `/` with the hreflange="x-default".

E.g.

```html
<link rel="alternate" hreflang="x-default" href="https://mysite.com/" />
<link rel="alternate" hreflang="en-nz" href="https://mysite.com/nz/" />
<link rel="alternate" hreflang="en-au" href="https://mysite.com/au/" />
```

It is recommended to leave this setting at the default false in order to ensure the correct
locale is set for every page, but in some cases (especially when upgrading websites) it
may be better to keep existing urls for the default locale intact.

Note: x-default is only added for the home pages. Other pages won't have x-default.

*Enabled behaviour*

If you prefer to keep the prefix off from all links in the default locale, you can set this option via
YML config. When this is enabled, the prefix will only be used (prepended) in links to non-default locales.

Expand All @@ -51,12 +76,16 @@ TractorCow\Fluent\Extension\FluentDirectorExtension:
disable_default_prefix: true
```
If this is left at the default, false, then the prefix will only be omitted for the
home page for the default locale, instead of all pages.
E.g.
```html
<link rel="alternate" hreflang="en-nz" href="https://mysite.com/" />
<link rel="alternate" hreflang="en-au" href="https://mysite.com/au/" />
```

Note: If you still wish to use x-default for the global locale, you can assign x-default
to the global locale via the CMS. However, this affects all pages, not just the home page.

It is recommended to leave this on in order to ensure the correct locale is set for every page,
but in some cases (especially when upgrading websites) it may be better to keep existing urls
for the default locale intact.

## Field localisation configuration

Expand Down
15 changes: 11 additions & 4 deletions src/Extension/FluentDirectorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class FluentDirectorExtension extends Extension
private static $query_param = 'l';

/**
* Allow the prefix for the default {@link Locale} (IsDefault = 1) to be disabled
* Allow the prefix for the default {@link Locale} (IsDefault = 1) to be disabled.
*
* If this is true, the `/' url will be the default locale. Note: You can also set this locale to use x-default
* via the CMS admin.
*
* If this is false, the `/` url will be 'x-default', and the default locale home page will be /urlsegment/.
*
* @config
* @var bool
Expand Down Expand Up @@ -91,9 +96,11 @@ public function updateRules(&$rules)
return;
}

// If we do not wish to detect the locale automatically, fix the home page route
// to the default locale for this domain.
if (!static::config()->get('detect_locale')) {
// If we don't want to detect home page locale, or the home page only has one candidate locale anyway,
if (!static::config()->get('detect_locale')
|| $defaultLocale->getIsOnlyLocale()
|| FluentDirectorExtension::config()->get('disable_default_prefix')
) {
// Respect existing home controller
$originalHomeRole = $originalRules[''] ?? null;
if ($originalHomeRole) {
Expand Down
30 changes: 25 additions & 5 deletions src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ public function MetaTags(&$tags)
$tags .= $this->owner->renderWith('FluentSiteTree_MetaTags');
}

/**
* If this is the site home page, but still has it's own non-root url,
* make sure we treat the root as x-default.
*
* @link https://github.com/tractorcow-farm/silverstripe-fluent/blob/master/docs/en/configuration.md#default-locale-options
*
* @return bool
*/
public function getLinkToXDefault()
{
// If we disable the prefix for the default locale, this will be the default instead
if (FluentDirectorExtension::config()->get('disable_default_prefix')) {
return false;
}

// If the current domain only has one locale, there is no x-default
$localeObj = $this->getRecordLocale();
if ($localeObj && $localeObj->getIsOnlyLocale()) {
return false;
}

// Only link to x-default on home page
return $this->owner->URLSegment === RootURLController::get_homepage_link();
}

/**
* Add the current locale's URL segment to the start of the URL
*
Expand Down Expand Up @@ -79,11 +104,6 @@ public function updateRelativeLink(&$base, &$action)

// Check if this locale is the default for its own domain
if ($localeObj->getIsDefault()) {
// For home page in the default locale, do not alter home URL
if ($base === null || $base === RootURLController::get_homepage_link()) {
return;
}

// If default locale shouldn't have prefix, then don't add prefix
if (FluentDirectorExtension::config()->get('disable_default_prefix')) {
return;
Expand Down
3 changes: 3 additions & 0 deletions templates/FluentSiteTree_MetaTags.ss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<% if $LinkToXDefault %>
<link rel="alternate" hreflang="x-default" href="{$absoluteBaseURL.ATT}" />
<% end_if %>
<% if $Locales %><% loop $Locales %><% if $IsPublished && $canViewInLocale %>
<link rel="alternate" hreflang="$HrefLang.ATT" href="$AbsoluteLink.ATT" />
<% end_if %><% end_loop %><% end_if %>
4 changes: 2 additions & 2 deletions tests/php/Extension/FluentSiteTreeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function provideURLTests()
{
return [
// Non-domain tests
[null, 'de_DE', false, 'home', '/'],
[null, 'de_DE', false, 'home', '/german/'],
[null, 'de_DE', false, 'about', '/german/about-us/'],
[null, 'de_DE', false, 'staff', '/german/about-us/my-staff/'],
// Since de_DE is the only locale on the www.example.de domain, ensure that the locale
Expand All @@ -131,7 +131,7 @@ public function provideURLTests()
['www.example.com', 'es_ES', false, 'about', '/es_ES/about-us/'],
['www.example.com', 'es_ES', false, 'staff', '/es_ES/about-us/my-staff/'],
// - en_US default locale
['www.example.com', 'en_US', false, 'home', '/'],
['www.example.com', 'en_US', false, 'home', '/usa/'],
['www.example.com', 'en_US', false, 'about', '/usa/about-us/'],
['www.example.com', 'en_US', false, 'staff', '/usa/about-us/my-staff/'],
// - en_US default locale, but with disable_default_prefix on
Expand Down

0 comments on commit 365b2cc

Please sign in to comment.