1.20.x translation.module translation_node_view(Node $node, $view_mode)

Implements hook_node_view().

Displays translation links with language names if this node is part of a translation set. If no language provider is enabled, "fall back" to simple links built through the result of translation_node_get_translations().

File

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

Code

function translation_node_view(Node $node, $view_mode) {
  // If the site has no translations or is not multilingual we have no content
  // translation links to display.
  // Also, don't display links if setting is disabled.
  $translation_show_links = config_get('node.type.' . $node->type, 'settings.translation_show_links');
  if ($translation_show_links && isset($node->tnid) && language_multilingual() && $translations = translation_node_get_translations($node->tnid)) {
    $languages = language_list(TRUE);

    // There might be a language provider enabled defining custom language
    // switch links which need to be taken into account while generating the
    // content translation links. As custom language switch links are available
    // only for configurable language types and interface language is the only
    // configurable language type in core, we use it as default. Contributed
    // modules can change this behavior by setting the config option below.
    $type = config_get('translation.settings', 'language_type');
    $custom_links = language_negotiation_get_switch_links($type, "node/$node->nid");
    $links = array();

    foreach ($translations as $langcode => $translation) {
      // Do not show links to the same node, to unpublished translations or to
      // translations in disabled languages.
      if ($translation->status && isset($languages[$langcode]) && $langcode != $node->langcode) {
        $language = $languages[$langcode];
        $key = "translation_$langcode";

        if (isset($custom_links->links[$langcode])) {
          $links[$key] = $custom_links->links[$langcode];
        }
        else {
          $links[$key] = array(
            'href' => "node/{$translation->nid}",
            'title' => isset($language->native) ? $language->native : $language->name,
            'language' => $language,
          );
        }

        // Custom switch links are more generic than content translation links,
        // hence we override existing attributes with the ones below.
        $links[$key] += array('attributes' => array());
        $attributes = array(
          'title' => $translation->title,
          'class' => array('translation-link'),
        );
        $links[$key]['attributes'] = $attributes + $links[$key]['attributes'];
      }
    }

    $node->content['links']['translation'] = array(
      '#theme' => 'links__node__translation',
      '#links' => $links,
      '#attributes' => array('class' => array('links', 'inline')),
    );
  }
}