You are here

function select_translation_of_node in Select translation 8

Same name and namespace in other branches
  1. 7 select_translation.module \select_translation_of_node()

Returns the selected translation of the given node.

The value of the mode string can be:

  • 'original', in which case the function will return a node following this preference order: + the node in the current language, if available; + the node in the original language, otherwise.
  • 'default', in which case the function will return a node following this preference order: + the node in the current language, if available; + the node in the default language, if available; + the node in the original language, otherwise.
  • a comma separated list of language codes, in which case it will return the first available node translation.

In that list, the special value 'current' refers to the current interface language, 'default' refers to the site default language, and 'original' to the node original language. If no match is found the node in the original language is returned.

Parameters

int $nid: The node id.

string $mode: The mode specifying how the translation should be selected.

Return value

\Drupal\node\Entity\Node The Node entity in the selected translation, or the original translation if one from $mode cannot be selected, or NULL if the node cannot be found.

1 call to select_translation_of_node()
drush_select_translation in drush/select_translation.drush.inc
Select which translation of a node should be displayed.

File

./select_translation.module, line 88
Main module file.

Code

function select_translation_of_node($nid, $mode = 'default') {
  $node = Node::load($nid);
  if (!$node) {
    return NULL;
  }
  $lang_list = select_translation_parse_mode($mode);
  foreach ($lang_list as $m) {
    if ($m == 'original') {
      return $node
        ->getUntranslated();
    }
    elseif ($node
      ->hasTranslation($m)) {
      return $node
        ->getTranslation($m);
    }
  }

  // Assuming that select_translation_parse_mode() always appends 'original'
  // to the list of languages, the code below should be unreachable.
  assert(FALSE);
  return NULL;
}