This site uses cookies and similar technologies.

If you don't change browser settings, you agree to it.

I understand

The field type "SEBLOD Canonical" works perfectly on single language site. 

On a multi language site it does the job well too, but only on content with a specific language. It does not work on content with language set to "All".

To overcome this issue you can add in the "Content View" position "Main Body" of your content a field 42 with the following code in "PrepareContent".

// collect information about the currently displayed article and the default site language from the Joomla global configuration

$uri = JURI::root();
$lang = JComponentHelper::getParams('com_languages')->get('site');
$artid = JFactory::getApplication()->input->getInt('id');

// define database object and query
$db = JFactory::getDBO();
$query = $db->getQuery(true);

// build query and put the result into an array
$query->select($db->quoteName(array('a.id','a.alias','a.language','b.id','b.alias'), array('idart','aliasart','languageart','idcat','aliascat')));
$query->from($db->quoteName('#__content','a'));
$query->join('LEFT', $db->quoteName('#__categories','b').' ON ('.$db->quoteName('a.catid').' = '.$db->quoteName('b.id').')');
$query->where($db->quoteName('a.id')." = ".$db->quote($artid));

// submit query
$db->setQuery($query);

// put the result into an associative array
$resultasc=$db->loadAssoc();

// get global document object
$doc = JFactory::getDocument();

// write canonical url tag into an articel with langauge "All"
if ($resultasc['languageart']=='*') {
$doc->addCustomTag('<link href="'.$uri.substr($lang,0,2).'/'.$resultasc['idcat'].'-'.$resultasc['aliascat'].'/'.$resultasc['idart'].'-'.$resultasc['aliasart'].'" '.'rel="canonical" />');

// write canonical url tag into an articel with a specific language
else {
$doc->addCustomTag('<link href="'.$uri.substr($resultasc['languageart'],0,2).'/'.$resultasc['idcat'].'-'.$resultasc['aliascat'].'/'.$resultasc['idart'].'-'.$resultasc['aliasart'].'" '.'rel="canonical" />');
}

How the code works: The code gets all the information of the article which is opened in the browser and the system (base URI and default site language). With a SQL query the ids and aliases of the article and category is retrieved. At the end, the custom canonical URL is put together and submitted to the header of the document.

The code works out of the box with two prerequisites:
- "SEO Settings" "Search Engine Friendly URLs" and "Use URL Rewriting" is set to "Yes" (mandantory)
- Article have a category different from "Uncategorised" (not mandantory but recommended.