Skip to content

Commit

Permalink
fix: allow overwriting of the Title field via customised data
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Leutenegger committed Sep 30, 2021
1 parent 93c2ded commit 463b197
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 25 additions & 6 deletions docs/en/01_Title.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<title>
<% if $SEOSource %>
$SEOSource.SEOTitle
<% else %>
$Title | $SiteConfig.Title
<% end_if %>
</title>
```
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

Expand Down
6 changes: 4 additions & 2 deletions src/Extensions/SEOSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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
Expand Down

0 comments on commit 463b197

Please sign in to comment.