Skip to content
Permalink
Browse files

Fix hreflang is more complete

  • Loading branch information
eldy committed Dec 7, 2019
1 parent 0e063a3 commit d58a6a7d9a5f31bd6397955645ba47d89e487a4e
Showing with 24 additions and 1 deletion.
  1. +24 −1 htdocs/core/lib/website2.lib.php
@@ -124,17 +124,40 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
$tplcontent .= '<meta name="dolibarr:pageid" content="'.dol_string_nohtmltag($objectpage->id).'" />'."\n";
// Add translation reference (main language)
if ($object->isMultiLang()) {
// Add myself
$tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="'.($object->fk_default_home == $objectpage->id ? '/' : '/'.$objectpage->pageurl.'.php').'" />'."\n";
// Add page "translation of"
$translationof = $objectpage->fk_page;
if ($translationof) {
$tmppage = new WebsitePage($db);
$tmppage->fetch($translationof);
if ($tmppage->id > 0) {
$tmpshortlangcode = '';
if ($tmppage->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : '/'.$tmppage->pageurl.'.php').'" />'."\n";
if ($tmpshortlangcode != $shortlangcode) {
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : '/'.$tmppage->pageurl.'.php').'" />'."\n";
}
}
}
// Add "has translation pages"
$sql = 'SELECT rowid as id, lang, pageurl from '.MAIN_DB_PREFIX.'website_page where fk_page IN ('.$objectpage->id.($translationof ? ", ".$translationof : "").")";
$resql = $db->query($sql);
if ($resql)
{
$num_rows = $db->num_rows($resql);
if ($num_rows > 0)
{
while ($obj = $db->fetch_object($resql))
{
$tmpshortlangcode = '';
if ($obj->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
if ($tmpshortlangcode != $shortlangcode) {
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $obj->id ? '/' : '/'.$obj->pageurl.'.php').'" />'."\n";
}
}
}
}
else dol_print_error($db);
}
// Add canonical reference
$tplcontent .= '<link href="/'.(($objectpage->id == $object->fk_default_home) ? '' : ($objectpage->pageurl.'.php')).'" rel="canonical" />'."\n";

0 comments on commit d58a6a7

Please sign in to comment.
You can’t perform that action at this time.