Skip to content

Commit

Permalink
Merge pull request #12 from lerni/metacomponnents
Browse files Browse the repository at this point in the history
utilize MetaComponents
  • Loading branch information
DorsetDigital authored Jul 12, 2024
2 parents bf15473 + 4257c06 commit cca072d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 52 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{ss,html,css,scss}]
indent_size = 4
indent_style = tab

[*.{yml,js,json,eslintrc,feature}]
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4
indent_style = tab
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# silverstripe-canonical
Adds a simple rel=canonical tag to Silverstripe 4
Adds a simple rel=canonical tag to Silverstripe

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/DorsetDigital/silverstripe-canonical/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/DorsetDigital/silverstripe-canonical/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/DorsetDigital/silverstripe-canonical/badges/build.png?b=master)](https://scrutinizer-ci.com/g/DorsetDigital/silverstripe-canonical/build-status/master)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE.md)
[![Version](http://img.shields.io/packagist/v/dorsetdigital/silverstripe-canonical.svg?style=flat)](https://packagist.org/packages/dorsetdigital/silverstripe-canonical)

# Requirements
*Silverstripe 4.x

(For a similar add-on for SS3, please see: lerni/canonical (https://github.com/lerni/canonical)

*Silverstripe 4 | 5

# Installation
* Install the code with `composer require dorsetdigital/silverstripe-canonical`
Expand All @@ -20,13 +17,13 @@ Adds a simple rel=canonical tag to Silverstripe 4
This module adds a simple canonical tag to your pages to allow for the specification of the default domain.
The canonical tag allows you to signal to search engines like Google which is the authoritative version of the page to help reduce duplicate content issues caused by non-www versions, pages with different protocols, etc.

Once installed, go to the site settings and enter the full canonical domain. This should include the protocol (eg. http:// or https://)
Once installed, go to the site settings and enter the full canonical domain. This should include the protocol (e.g. http:// or https://)

The module tries to be the last extension to be applied, and will try to remove any existing `rel="canonical"` tags from the head.


At the time of writing, the canonical tags are only added correctly for pages in the SiteTree. Pages generated from custom controllers or from DataObjects may or may not work! When creating the canonical tags, the module will look for a method called `CanonicalLink()` on the extended object. If you are using DataObjects as pages, adding this method on the relevant controller should allow you to control the canonical URL in these cases.

# Additional Credits
* Thanks to @sanderha (https://github.com/sanderha) for the Danish translation
* Thanks to @lerni (https://github.com/lerni) for his enhancements
* Thanks to @sanderha for the Danish translation
* Thanks to @lerni for his enhancements
52 changes: 28 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{
"name": "dorsetdigital/silverstripe-canonical",
"description": "Adds a simple canonical tag in Silverstripe 4",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/DorsetDigital/silverstripe-canonical",
"keywords": ["silverstripe", "html", "seo", "canonical"],
"authors": [
{
"name": "Tim Burt",
"email": "tim@dorset-digital.net"
}
],
"require": {
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/cms": "^4.0"
},
"license": "BSD-3-Clause",
"autoload": {
"psr-4": {
"DorsetDigital\\SilverStripeCanonical\\": "src/"
}
},
"extra": {
},
"minimum-stability": "dev"
"name": "dorsetdigital/silverstripe-canonical",
"description": "Adds a simple canonical tag in Silverstripe 4",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/DorsetDigital/silverstripe-canonical",
"keywords": [
"silverstripe",
"html",
"seo",
"canonical"
],
"authors": [
{
"name": "Tim Burt",
"email": "tim@dorset-digital.net"
}
],
"require": {
"silverstripe/vendor-plugin": "^1.0 || ^2.0",
"silverstripe/cms": "^4.0 || ^5.0"
},
"license": "BSD-3-Clause",
"autoload": {
"psr-4": {
"DorsetDigital\\SilverStripeCanonical\\": "src/"
}
},
"extra": {},
"minimum-stability": "dev"
}
3 changes: 2 additions & 1 deletion lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ de:
DorsetDigital\SilverStripeCanonical\SiteTreeCanonicalExtension:
LinkOverride: 'Canonical URL übersteuern'
LinkOverrideDesc: 'Nur verwenden, wenn eine andere URL als "Original" gekennzeichnet werden soll.'
LinkOverrideVirtualDesc: 'Die verlinkte Seite wird verwendet.'
LinkFieldPlaceholder: 'Canonical-URLs benötigt eine Canoinical-Domain in <a href="/admin/settings">SiteConfig</a>'
LinkFieldRightTitle: 'Wird verwendet um die Originalresource (URL) auszuweisen und "Duplicate Content" zu verhindern.'
LinkFieldRightTitle: 'Wird verwendet um die Originalresource (URL) auszuweisen und "Duplicate Content" zu verhindern.'
37 changes: 18 additions & 19 deletions src/SiteTreeCanonicalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Forms\LiteralField;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\CMS\Model\VirtualPage;

class SiteTreeCanonicalExtension extends SiteTreeExtension
{
Expand All @@ -23,6 +24,11 @@ public function updateCMSFields(FieldList $fields)
$MetaCanonical
->setAttribute('placeholder', $this->getorsetCanonicalURL())
->setDescription(_t(__CLASS__ . '.LinkOverrideDesc', 'Only set this if another URL should count as the original (e.g. of reposting a blog post from another source).'));
if ($this->owner->ClassName == VirtualPage::class) {
$MetaCanonical
->setReadonly(true)
->setDescription(_t(__CLASS__ . '.LinkOverrideVirtualDesc', 'Linked page will be used.'));
}
} else {
$MetaToggle->push($MetaCanonical = LiteralField::create("CanonicalURL", '<p class="form__field-label">' . _t(__CLASS__ . '.LinkFieldPlaceholder', 'Canonical-URLs needs a Canoinical-Domain in <a href="/admin/settings">SiteConfig</a>') . '</p>'));
}
Expand All @@ -46,6 +52,11 @@ function getorsetCanonicalURL()
$link = $this->owner->CanonicalURL;
}

// use CopyContentFrom()->Link() for VirtualPage
if ($this->owner->ClassName == VirtualPage::class) {
$link = $this->owner->CopyContentFrom()->Link();
}

// add canonicalBase if relative URL
if (isset($link)) {
$urlArray = parse_url($link);
Expand All @@ -61,28 +72,16 @@ function getorsetCanonicalURL()
}
}

public function MetaTags(&$tags)
public function MetaComponents(array &$tags)
{
if ($canonLink = $this->getorsetCanonicalURL()) {
$atts = [
'rel' => 'canonical',
'href' => $canonLink
$tags['canonical'] = [
'tag' => 'link',
'attributes' => [
'rel' => 'canonical',
'href' => $canonLink
]
];
$canonTag = HTML::createTag('link', $atts);

$tagsArray = explode(PHP_EOL, $tags);
$tagPattern = 'rel="canonical"';

$tagSearch = function ($val) use ($tagPattern) {
return (stripos($val, $tagPattern) !== false ? true : false);
};

$currentTags = array_filter($tagsArray, $tagSearch);
$cleanedTags = array_diff($tagsArray, $currentTags);

$cleanedTags[] = $canonTag;

$tags = implode(PHP_EOL, $cleanedTags);
}
}
}

0 comments on commit cca072d

Please sign in to comment.