Skip to content

Commit

Permalink
Support x-default locale code for SEO
Browse files Browse the repository at this point in the history
Sortable Locales
  • Loading branch information
tractorcow committed Feb 14, 2020
1 parent d864f5b commit f55a0ea
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 26 deletions.
17 changes: 17 additions & 0 deletions src/Control/LocaleAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace TractorCow\Fluent\Control;

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\View\Requirements;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use TractorCow\Fluent\Extension\FluentDirectorExtension;
use TractorCow\Fluent\Extension\FluentMemberExtension;
use TractorCow\Fluent\Model\Domain;
Expand Down Expand Up @@ -36,6 +38,21 @@ protected function init()
Requirements::css("tractorcow/silverstripe-fluent:client/dist/styles/fluent.css");
}

public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);

// Add sortable field to locales
if ($this->modelClass === Locale::class) {
/** @var GridField $listField */
$listField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass));
$config = $listField->getConfig();
$config->addComponent(new GridFieldOrderableRows('Sort'));
}

return $form;
}

public function getClientConfig()
{
/** @var Member|FluentMemberExtension $member */
Expand Down
33 changes: 32 additions & 1 deletion src/Model/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @property string $URLSegment
* @property bool $IsGlobalDefault
* @property int $DomainID
* @property bool $UseDefaultCode
* @method HasManyList|FallbackLocale[] FallbackLocales()
* @method ManyManyList|Locale[] Fallbacks()
* @method Domain Domain() Raw SQL Domain (unfiltered by domain mode)
Expand Down Expand Up @@ -62,6 +63,13 @@ class Locale extends DataObject implements PermissionProvider

private static $plural_name = 'Locales';

/**
* hreflang for default landing pages.
* Note: PHP's ext-intl doesn't support this code, so only use it
* in templates.
*/
const X_DEFAULT = 'x-default';

private static $summary_fields = [
'Title' => 'Title',
'Locale' => 'Locale',
Expand All @@ -79,9 +87,11 @@ class Locale extends DataObject implements PermissionProvider
'Locale' => 'Varchar(10)',
'URLSegment' => 'Varchar(100)',
'IsGlobalDefault' => 'Boolean',
'UseDefaultCode' => 'Boolean',
'Sort' => 'Int',
];

private static $default_sort = '"Fluent_Locale"."Locale" ASC';
private static $default_sort = '"Fluent_Locale"."Sort" ASC, "Fluent_Locale"."Locale" ASC';

/**
* @config
Expand Down Expand Up @@ -203,6 +213,19 @@ public function getBadgeLabel()
return (string)$badgeLabel;
}

/**
* RFC 1766 hreflang
*
* @return string
*/
public function getHrefLang()
{
if ($this->UseDefaultCode) {
return self::X_DEFAULT;
}
return i18n::convert_rfc1766($this->Locale);
}

/**
* Get URLSegment for this locale
*
Expand Down Expand Up @@ -250,6 +273,14 @@ public function getCMSFields()
'Note: Per-domain specific locale can be assigned on the Locales tab'
. ' and will override this value for specific domains.'
)),
CheckboxField::create(
'UseDefaultCode',
_t(__CLASS__ . '.USE_X_DEFAULT', 'Use {code} as hreflang', ['code' => self::X_DEFAULT])
)
->setDescription(_t(
__CLASS__ . '.USE_X_DEFAULT_DESCRIPTION',
'Use of this code indicates to search engine that this is a non-localised global landing page'
)),
DropdownField::create(
'DomainID',
_t(__CLASS__ . '.DOMAIN', 'Domain'),
Expand Down
15 changes: 13 additions & 2 deletions src/Model/RecordLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RecordLocale extends ViewableData
* FluentLocale constructor.
*
* @param DataObject $record
* @param Locale $locale
* @param Locale $locale
*/
public function __construct(DataObject $record, Locale $locale)
{
Expand Down Expand Up @@ -114,7 +114,18 @@ public function getLocale()
*/
public function getLocaleRFC1766()
{
return i18n::convert_rfc1766($this->getLocale());
return $this->getHrefLang();
}

/**
* Get hreflang field
*
* @return string
*/
public function getHrefLang()
{
$locale = $this->getLocaleObject();
return $locale->getHrefLang();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/FluentSiteTree_MetaTags.ss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<% if $Locales %><% loop $Locales %><% if $IsPublished && $canViewInLocale %>
<link rel="alternate" hreflang="$LocaleRFC1766.ATT" href="$AbsoluteLink.ATT" />
<link rel="alternate" hreflang="$HrefLang.ATT" href="$AbsoluteLink.ATT" />
<% end_if %><% end_loop %><% end_if %>
23 changes: 12 additions & 11 deletions templates/LocaleMenu.ss
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<% if $Locales %>
<div class="left">Locale <span class="arrow">&rarr;</span>
<nav class="primary">
<ul>
<% loop $Locales %>
<li class="$LinkingMode">
<a href="$Link.ATT" <% if $LinkingMode != 'invalid' %>rel="alternate" hreflang="$LocaleRFC1766"<% end_if %>>$Title.XML</a>
</li>
<% end_loop %>
</ul>
</nav>
</div>
<div class="left">Locale <span class="arrow">&rarr;</span>
<nav class="primary">
<ul>
<% loop $Locales %>
<li class="$LinkingMode">
<a href="$Link.ATT" <% if $LinkingMode != 'invalid' %>rel="alternate"
hreflang="$HrefLang"<% end_if %>>$Title.XML</a>
</li>
<% end_loop %>
</ul>
</nav>
</div>
<% end_if %>
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='{$BaseHref}sitemap.xml/styleSheet'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<% loop $Items %>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>
<% loop $Items %>
<url>
<loc>$AbsoluteLink</loc>
<% if $LastEdited %><lastmod>$LastEdited.Rfc3339()</lastmod><% end_if %>
<% if $ChangeFrequency %><changefreq>$ChangeFrequency</changefreq><% end_if %>
<% if $GooglePriority %><priority>$GooglePriority</priority><% end_if %>
<% if $LastEdited %>
<lastmod>$LastEdited.Rfc3339()</lastmod><% end_if %>
<% if $ChangeFrequency %>
<changefreq>$ChangeFrequency</changefreq><% end_if %>
<% if $GooglePriority %>
<priority>$GooglePriority</priority><% end_if %>
<% if $Locales %><% loop $Locales %><% if $LinkingMode != 'invalid' %>
<xhtml:link
rel="alternate"
hreflang="$LocaleRFC1766"
href="$AbsoluteLink"
<xhtml:link
rel="alternate"
hreflang="$HrefLang"
href="$AbsoluteLink"
/>
<% end_if %><% end_loop %><% end_if %>
<% if $ImagesForSitemap %><% loop $ImagesForSitemap %>
Expand All @@ -20,5 +26,5 @@
</image:image>
<% end_loop %><% end_if %>
</url>
<% end_loop %>
<% end_loop %>
</urlset>
3 changes: 2 additions & 1 deletion tests/php/Extension/FluentSiteTreeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function testGetLocaleInformation()
$this->assertInstanceOf(RecordLocale::class, $result);
$this->assertEquals('en_NZ', $result->getLocale());
$this->assertEquals('en-NZ', $result->getLocaleRFC1766());
$this->assertEquals('en-NZ', $result->getHrefLang());
$this->assertEquals('English (New Zealand)', $result->getTitle());
$this->assertEquals('English', $result->getLanguageNative());
$this->assertEquals('en', $result->getLanguage());
Expand Down Expand Up @@ -154,7 +155,7 @@ public function provideURLTests()
* @dataProvider provideURLTests
* @param string $domain
* @param string $locale
* @param bool $prefixDisabled
* @param bool $prefixDisabled
* @param string $pageName
* @param string $url
*/
Expand Down

0 comments on commit f55a0ea

Please sign in to comment.