I have been thinking abut getting Canonical link rel's to work with modules and here it is, with some provisos, and on the work that others have previously done. Note I'm no Smarty expert!
1. In the News Details template:
Code: Select all
{assign var="newsarticle" value=$entry->title}
{assign var="entryid" value=$entry->id}
You can add these to other templates for e.g. Calendar or Album. However they will need some alteration for the module variables.
2. In your Page Template before you set the canonical link rel:
Code: Select all
{capture assign='newscontent'}{content}{/capture}
{if isset($newsarticle)}
{if ($page_alias eq 'news')}
{capture assign='ModuleCanonical'}<link rel="canonical" href="{root_url}/{$page_alias}/{$entryid}/{$page_id}/{$newsarticle|replace:" ":"-"}/" />{/capture}
{/if}{/if}
You can ignore this
Again I guess you need different captures for different modules. Also if your url is in a different format or just a different module, like for Album or Calendar, then you need to do a bit of editing of the 'ModuleCanonical' assignment. (I know for some modules that you can add ready made variables for the ModuleCanonical link - but I broke it down so people could see what was happening. I am also not sure you still need the '{capture assign='newscontent'}{content}{/capture}' stuff with the new CMCMS and reading of the whole page - but it works and perhaps someone could suggest a better way of doing it?
I also think maybe having lots of
{capture assign='newscontent'}{content}{/capture}
....
and
{capture assign='eventcontent'}{content}{/capture}
...
is going to slow things down? Especially if you need one for News/Calendar/Album/ etc.

)
I would also limit where you are want the main news to be, the '{if ($page_alias eq 'news')}' bit. For example, if your news index is off the root then fine and dandy, but if you also put it on you home page - what happens - look at the links - click them and then look at the Canonical url in the source - you can easily end up with invalid URLs there! For news at least, it is best to wrap with a page alias check.
Also maybe hard code your return id to you main news page id
e.g in the News module action.default.php on line six add
$returnid=82; (or whatever your main news page id is.)
It is clumsy - but works -anyone know a better way?
You could also capture/change/write the page title in the template with this smarty, just a few extra lines...
3. And where you want the Canonical link to appear in your Page Template, below the above smarty code;-)
Code: Select all
<!-- SEO Canonical -->
{if isset($ModuleCanonical)}{$ModuleCanonical}
{else}{GetCanonical}
{/if}
4. This above assumes you have a UDT Called 'GetCanonical' or you can change the name for whatever you like.
UDT from above post:
Code: Select all
global $gCms;
if ($gCms->variables['page_name'] !='') {
$manager =& $gCms->GetHierarchyManager();
$node =& $manager->sureGetNodeByAlias($gCms->variables['page_id']);
$content =& $node->GetContent();
if (isset($content) && is_object($content))
{
if ($content->GetURL() != '')
{
echo '<link rel="canonical" href="'.$content->GetURL().'" />';
}
}
}
Comments and suggestions welcome.
Russ