You are here

hreflang_hierarchy.module in Language Hierarchy 7

File

modules/hreflang_hierarchy/hreflang_hierarchy.module
View source
<?php

/**
 * Implements hook_html_head_alter().
 */
function hreflang_hierarchy_html_head_alter(&$head_elements) {
  $current_path = current_path();
  if (path_is_admin($current_path)) {
    return;
  }

  // Get path and translations
  $path = $_GET['q'];

  // Add hreflang="x-default" for front page.
  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);

  // Get disabled languages
  $languages = language_list('enabled');

  // Other link types like panels pages or views.
  if (empty($translations)) {
    $translations = array();
    foreach ($languages[1] as $language) {
      $alias = drupal_get_path_alias($path, $language->language);
      $translations[$language->language] = array(
        'href' => $alias,
      );
    }
  }

  // Get alternate urls if we've got translations.
  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,
      );

      // Build the hreflang element.
      $element = array(
        '#tag' => 'link',
        '#attributes' => $attributes,
        '#type' => 'html_tag',
      );
      $key = 'hreflang:' . $langcode . '/' . $translation['href'];
      $head_elements[$key] = $element;
    }
  }
}

/**
 * Gets hreflang links for given path.
 *
 * @param $path
 *   Path used to lookup for hreflang links.
 *
 * @return array
 *   Hreflang links.
 */
function hreflang_hierarchy_get_links($path) {
  $languages = language_list('enabled');
  $links = array();

  // Node entity.
  $entity = menu_get_object();
  $entity_type = 'node';

  // Taxonomy term entity.
  if (empty($entity)) {
    $entity = menu_get_object('taxonomy_term', 2);
    $entity_type = 'taxonomy_term';
  }

  // User entity.
  if (empty($entity)) {
    $entity = menu_get_object('user');
    $entity_type = 'user';
  }

  // Entity translation support.
  // Entity translation is enabled for loaded entity and it has some translations.
  $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;
}

/**
 * Implements hook_language_switch_links_alter().
 */
function hreflang_hierarchy_language_switch_links_alter(array &$links, $type, $path) {
  foreach ($links as $langcode => &$link) {
    $link['attributes']['hreflang'] = $langcode;
  }
}