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

2

9

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.

Ri answered 9/2, 2011 at 20:28 Comment(0)
R
13
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.

Ri answered 11/2, 2011 at 19:10 Comment(0)
R
1

Drupal 8

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

foreach ($languages as $langcode => $language) {
  $translations[$langcode] = $node->getTranslation($langcode);
}
Remains answered 10/1, 2022 at 23:21 Comment(1)
I am sure it would help the community if you explained to us why your code would solve the OP's problemSmoothshaven

© 2022 - 2024 — McMap. All rights reserved.