Add hreflang links to the head.

Public

For Entity Translation

Get raw version
php
  1. /**
  2.  * Implements hook_init().
  3.  *
  4.  * Add hreflang links to the head.
  5.  * See https://support.google.com/webmasters/answer/189077
  6.  */
  7. function MYMODULE_init() {
  8. $node = menu_get_object();
  9. if ($node) {
  10. global $language;
  11. foreach ($node->translations->data as $langcode => $translation) {
  12. if ($langcode !== $language->language) {
  13. $attributes = array(
  14. 'href' => url('node/' . $node->nid, array('absolute' => TRUE, 'language' => i18n_language($langcode))),
  15. 'rel' => 'alternate',
  16. 'hreflang' => $langcode,
  17. );
  18. drupal_add_html_head_link($attributes);
  19. }
  20. }
  21. }
  22. }

For Content Translation

Get raw version
php
  1. /**
  2.  * Implements hook_init().
  3.  *
  4.  * Add hreflang links to the head.
  5.  * See https://support.google.com/webmasters/answer/189077
  6.  */
  7. function MYMODULE_init() {
  8. $node = menu_get_object();
  9. if ($node && isset($node->tnid)) {
  10. global $language;
  11. $translations = translation_node_get_translations($node->tnid);
  12. foreach ($translations as $langcode => $translation) {
  13. if ($langcode !== $language->language) {
  14. $attributes = array(
  15. 'href' => url('node/' . $translation->nid, array('absolute' => TRUE, 'language' => i18n_language($langcode))),
  16. 'rel' => 'alternate',
  17. 'hreflang' => $langcode,
  18. );
  19. drupal_add_html_head_link($attributes);
  20. }
  21. }
  22. }
  23. }