HTML Link element with rel="alternate" for multilingual Drupal website
<?php | |
/** | |
* More info : http://www.google.com/support/webmasters/bin/answer.py?answer=189077&hl=en | |
*/ | |
/** | |
* Implements hook_preprocess_page() | |
*/ | |
function mymodule_preprocess_page(&$vars) { | |
global $language; | |
// Get enabled languages, and remove current | |
$languages = array_pop(language_list('enabled')); | |
unset($languages[$language->language]); | |
// Get path | |
$path = drupal_is_front_page() ? '<front>' : $_GET['q']; | |
// Get urls | |
$links = array(); | |
foreach($languages as $lang) { | |
$links[$lang->language] = array( | |
'href' => $path, | |
'language' => $lang, | |
'absolute' => TRUE, | |
); | |
} | |
drupal_alter('translation_link', $links, $path); | |
// Set head tags | |
foreach($links as $link) { | |
drupal_set_html_head('<link rel="alternate" hreflang="'. $link['language']->language .'" href="'. url($link['href'], $link) .'" />'); | |
} | |
// Update head | |
$vars['head'] = drupal_get_html_head(); | |
} |
This comment has been minimized.
This comment has been minimized.
In my case I had to use array_shift() instead of array_pop() at the line where language_list('enabled') is called, otherwise I get the disabled languages ;-) |
This comment has been minimized.
This comment has been minimized.
On a second thought, a better solution is to replace this line with the following two: $languages = language_list('enabled'); This will solve the issue no matter where the position of the enabled languages is in the array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Last google info : http://googlewebmastercentral.blogspot.com/2011/12/new-markup-for-multilingual-content.html