Skip to content

Commit

Permalink
Merge pull request #122 from muskie9/pull/seoBlogCanonical
Browse files Browse the repository at this point in the history
UPDATE set canonical in cases where there is category/tag/pagination
  • Loading branch information
muskie9 authored Sep 12, 2021
2 parents ab498b9 + aa0c4d6 commit 047045e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Extension/BlogDataExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Dynamic\Base\Extension;

use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\PaginatedList;

/**
*
*/
class BlogDataExtension extends DataExtension
{
/**
* @param $tags
*/
public function MetaComponents(&$tags)
{
$request = Controller::curr()->getRequest();
$action = $request->param('Action');
$id = $request->param('ID');
$var = PaginatedList::create(ArrayList::create())->getPaginationGetVar();

if (($action && $id) || $request->getVar($var)) {
$link = Controller::join_links(Director::absoluteBaseURL(), $this->owner->Link($action), $id);

$tags['canonical'] = [
'attributes' => [
'rel' => 'canonical',
'content' => $link,
],
];
}
}
}
24 changes: 24 additions & 0 deletions src/Extension/BlogPostDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\PaginatedList;

/**
* Class BlogPostDataExtension
Expand Down Expand Up @@ -49,6 +53,26 @@ public function updateCMSFields(FieldList $fields)
$fields->insertBefore('Content', $featured);
}

/**
* @param $tags
*/
public function MetaComponents(&$tags)
{
$request = Controller::curr()->getRequest();
$vars = $request->getVars();

if (count(preg_grep('/^commentsstart[0-9]+/', array_keys($vars)))) {
$link = Controller::join_links(Director::absoluteBaseURL(), $this->owner->Link());

$tags['canonical'] = [
'attributes' => [
'rel' => 'canonical',
'content' => $link,
],
];
}
}

/**
* @return DataList
*/
Expand Down

0 comments on commit 047045e

Please sign in to comment.