View source
<?php
function hreflang_hierarchy_html_head_alter(&$head_elements) {
$current_path = current_path();
if (path_is_admin($current_path)) {
return;
}
$path = $_GET['q'];
if (drupal_is_front_page()) {
$path = '<front>';
$element = array(
'#tag' => 'link',
'#attributes' => array(
'href' => url($path),
'rel' => 'alternate',
'hreflang' => 'x-default',
),
'#type' => 'html_tag',
);
$head_elements['hreflang:x-default'] = $element;
}
$translations = hreflang_hierarchy_get_links($path);
$languages = language_list('enabled');
if (empty($translations)) {
$translations = array();
foreach ($languages[1] as $language) {
$alias = drupal_get_path_alias($path, $language->language);
$translations[$language->language] = array(
'href' => $alias,
);
}
}
if ($translations) {
foreach ($translations as $langcode => $translation) {
if (!empty($languages[1][$langcode]->hidden)) {
continue;
}
$attributes = array(
'href' => url($translation['href'], $translation + array(
'language' => $languages[1][$langcode],
)),
'rel' => 'alternate',
'hreflang' => $langcode,
);
$element = array(
'#tag' => 'link',
'#attributes' => $attributes,
'#type' => 'html_tag',
);
$key = 'hreflang:' . $langcode . '/' . $translation['href'];
$head_elements[$key] = $element;
}
}
}
function hreflang_hierarchy_get_links($path) {
$languages = language_list('enabled');
$links = array();
$entity = menu_get_object();
$entity_type = 'node';
if (empty($entity)) {
$entity = menu_get_object('taxonomy_term', 2);
$entity_type = 'taxonomy_term';
}
if (empty($entity)) {
$entity = menu_get_object('user');
$entity_type = 'user';
}
$et_enabled = $entity && module_exists('entity_translation_hierarchy') && entity_translation_enabled($entity_type, $entity);
$has_translation = isset($entity->translations->data);
if ($et_enabled && $has_translation) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
foreach ($languages[1] as $language) {
$language_candidate = entity_translation_hierarchy_get_candidate($entity, $entity_type, $language->language);
if (!entity_translation_hierarchy_is_blocked($entity, $entity_type, $language_candidate) && empty($language->hidden)) {
$links[$language->language] = array(
'href' => $entity_type . '/' . $entity_id,
'object' => $entity->translations->data[$language_candidate],
);
}
}
}
elseif (module_exists('translation') && !empty($entity->tnid)) {
$translations = translation_node_get_translations($entity->tnid);
foreach ($translations as $langcode => $translation) {
if ($langcode != $entity->language && $translation->status && isset($languages[1][$langcode]) && empty($languages[1][$langcode]->hidden)) {
$links[$langcode] = array(
'href' => 'node/' . $translation->nid,
'object' => $translation,
);
}
}
}
else {
foreach ($languages[1] as $language) {
if (empty($language->hidden)) {
$links[$language->language] = array(
'href' => $path,
'title' => $language->native,
);
}
}
}
return $links;
}
function hreflang_hierarchy_language_switch_links_alter(array &$links, $type, $path) {
foreach ($links as $langcode => &$link) {
$link['attributes']['hreflang'] = $langcode;
}
}