function translation_node_view

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

drupal/core/modules/translation/translation.module, line 251
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.
  if (isset($node->tnid) && language_multilingual() && ($translations = translation_node_get_translations($node->tnid))) {
    $languages = language_list(LANGUAGE_ALL);

    // 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 system variable below.
    $type = config('translation.settings')
      ->get('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 or to unpublished translations.
      if ($translation->status && isset($languages[$langcode]) && $langcode != $node->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' => language_name($langcode),
            'language' => $languages[$langcode],
          );
        }

        // 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',
        ),
      ),
    );
  }
}