1.20.x translation.module translation_language_switch_links_alter(array &$links, $type, $path)

Implements hook_language_switch_links_alter().

Replaces links with pointers to translated versions of the content.

File

modules/translation/translation.module, line 590
Manages content translations.

Code

function translation_language_switch_links_alter(array &$links, $type, $path) {
  $language_type = config_get('translation.settings', 'language_type');

  if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
    $node = node_load((int) $matches[1]);

    if (empty($node->tnid)) {
      // If the node cannot be found nothing needs to be done. If it does not
      // have translations it might be a language neutral node, in which case we
      // must leave the language switch links unaltered. This is true also for
      // nodes not having translation support enabled.
      if (empty($node) || $node->langcode == LANGUAGE_NONE || !translation_supported_type($node->type)) {
        return;
      }
      $translations = array($node->langcode => $node);
    }
    else {
      $translations = translation_node_get_translations($node->tnid);
    }

    foreach ($links as $langcode => $link) {
      if (isset($translations[$langcode]) && $translations[$langcode]->status) {
        // Translation in a different node.
        $links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
      }
      else {
        // No translation in this language, or no permission to view.
        unset($links[$langcode]['href']);
        $links[$langcode]['attributes']['class'][] = 'locale-untranslated';
      }
    }
  }
}