Programmatically add node translation

When I was trying to add a node translation, it took me a moment to find out the the tnid of the node has to be equal to its nid. By writing about it here I hope others won't have to search as long and far as I did ;).

// Define which node is the source, this node has his own id as translation nid (tnid)
$source_node = node_load($source_nid);
$source_node->tnid = $source_nid;
node_save($source_node);

$new_node_in_a_other_language = new stdClass();
$new_node_in_a_other_language->type = 'your_type';
node_object_prepare($new_node_in_a_other_language);
$new_node_in_a_other_language->title    = 'title fr';
$new_node_in_a_other_language->language = 'fr';
// define source tnid  
$new_node_in_a_other_language->tnid = $source_nid;
node_save($new_node_in_a_other_language);

source: http://drupal.stackexchange.com/questions/23410/programmatically-add-nod...

Tags:

Share: