Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Drupal how to get tnid or the node id of the translated node?

I need to access the id of the translated node, if available for any given node. nid is the node id. It would seem tnid would be the id of the translated node. However, that seem not be the case. How can I get that id? I tried to the following code, which did not work.

  global $language;
  $translations = translation_node_get_translations($node->tnid);
  if ($translations[$language->language]) {
  $tnode = node_load($translations[$language->language]->nid);
  echo $tnode->nid;
  }

Any suggestions?

I need tnid to create a custom translation-link. Thanks.

like image 413
Natkeeran Avatar asked Feb 09 '11 20:02

Natkeeran


2 Answers

translation_node_get_translations($node->tnid);

Provides the array of all the corresponding language nodes. I did not realize it, but that's all I needed.

like image 92
Natkeeran Avatar answered Nov 04 '22 18:11

Natkeeran


Drupal 8

$languages = $node->getTranslationLanguages();
$translations = [];

foreach ($languages as $langcode => $language) {
  $translations[$langcode] = $node->getTranslation($langcode);
}
like image 22
Oleg Kreminskyy Avatar answered Nov 04 '22 16:11

Oleg Kreminskyy