From 463b1970ae425ec7098a7df4199455c02ecc7ce8 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Thu, 30 Sep 2021 18:02:02 +0200 Subject: [PATCH] fix: allow overwriting of the Title field via customised data --- docs/en/01_Title.md | 31 ++++++++++++++++++++----- src/Extensions/SEOSiteTreeExtension.php | 6 +++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/docs/en/01_Title.md b/docs/en/01_Title.md index 9d635ab..6f7fad7 100644 --- a/docs/en/01_Title.md +++ b/docs/en/01_Title.md @@ -4,13 +4,32 @@ are vital properties in order to be placed well in a google search. With silverstripe cms, you typically let the framework render the title tag automatically (which corresponds to the page name) or overwrite the default -title in the `Page.ss` template. While both strategies work, we have found that, -especially with page objects, the title tag is often different from the page name. +title in the `Page.ss` template. -We therefore add an additional field to the page object, the `meta title`, which -lets an editor overwrite the title specifically. This is the default setup when -installing this module, but you can change this behaviour by using the following -configuration options: +We have found that, especially with page objects, the title tag is often +different from the page name. Therefore, this module adds an additional field to +the page object, the `meta title`, which lets an editor overwrite the title +specifically. + +While you can let Silverstripe handle the rendering of the title tag using the +`$MetaTags` in the head section, this may become unwieldy when combined with +controllers that try to overwrite the Title, as this Title is then not picked +up by the module. We therefore recommend to render the title in your `Page.ss` +template as follows: + +```html + $MetaTags(false) + + <% if $SEOSource %> + $SEOSource.SEOTitle + <% else %> + $Title | $SiteConfig.Title + <% end_if %> + +``` +This will only render a specific title when a source has been set (in the DOAP +case) or when the current controller action is `index` (in the normal case of +rendering a page). Otherwise, the title will fall back to the normal behaviour. ## Disable the metatitle field diff --git a/src/Extensions/SEOSiteTreeExtension.php b/src/Extensions/SEOSiteTreeExtension.php index 4303cbd..080c522 100644 --- a/src/Extensions/SEOSiteTreeExtension.php +++ b/src/Extensions/SEOSiteTreeExtension.php @@ -3,6 +3,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataExtension; +use SilverStripe\Control\Controller; use SilverStripe\CMS\Model\SiteTree; use Syntro\SEO\Extensions\SEOExtension; @@ -28,11 +29,12 @@ public function getSEOSource() { if ($this->source) { return $this->source; - } else { + } else if (Controller::curr()->getAction() == 'index') { $owner = $this->getOwner(); $this->setSEOSource($owner); return $owner; } + return null; } /** @@ -57,7 +59,7 @@ public function MetaComponents(&$tags) /** @var SiteTree $owner */ $owner = $this->getOwner(); $source = $this->getSEOSource(); - if (!$source->hasExtension(SEOExtension::class)) { + if (!$source || !$source->hasExtension(SEOExtension::class)) { return $tags; } // Add a title