3

I want to be able to clone/replicate a node from one language to another on click of a button using a custom module. Basically we have a view which displays all nodes in the system and under the "operations" column, we have Edit, Delete, Clone.

On click of the Clone button, I want to replicate the node (keeping the same exact data values as is except for the language field) to other languages.

I have installed the node_clone and replicate modules to help me, but I am struggling to see how I can create a custom module that can call the hooks in those contrib modules in such a way that I can save the node in different languages automatically instead of click on the "Translate" tab and manually doing it one by one.

I have tried calling the clone_node_save from the Node clone module from my custom module, but that does not work.

How can I extend a module hook in a custom module?

3 Answers 3

3

You can use the following code to solve your problem:

//clone the node
$new = clone $node;
//set the node to new
$new->is_new = true;
//unset the nid and the vid
unset($new->nid);
unset($new->vid);
//setting the new language
$new->language = 'fr';
//save the new node
node_save($new);

Reference: Programmatically create a new node from an existing NID

2
  • This is awesome, thank you! Would it be advised to use the batch api as well so that way once the data is saved to the database i can get all the fields copied to the appropriate languages? Commented Mar 12, 2016 at 8:08
  • If you do not want to have the cloned node connected to the initial node as translation this is the way to go.
    – schlicki
    Commented Mar 12, 2016 at 8:12
2

I assume you're triggering the clone using the "clone item" VBO action provided by the node_clone module? If so, have you looked at hook_clone_node_alter()? It gives you the node (and some context data - see clone_action_clone() in clone.module) and lets you change values before the cloned node is saved.

If you are not using the VBO action, and therefore don't have the hook available, you might want to look at clone_action_clone() and copy the relevant code.

1
  • I shall look at the clone_action_clone as I am not using VBO Commented Mar 12, 2016 at 8:07
2

I think you don't need the node clone module. Look at this solution from the comment: https://www.drupal.org/node/1069774#comment-4834192 This is actually utilizing the entity translation module's translation handler. Here is the code again:

<?php

$translation = array(
  'translate' => 0,
  'status' => 1,
  'language' => 'sv', // here is the language you're translating to
  'source' => 'en', // here is the source language
);

$node = node_load($nid); // If you've called node_save($node) before, you'll have the node object already and you can omit this
$handler = entity_translation_get_handler('node', $node);    
$values['name_field']['sv']['0']['value'] = $sv_name; // You can write the array like this, I find it easier to read
$handler->setTranslation($translation, $values);
field_attach_update('node', $node);

?>

Create a module with hook_menu and a page callback that holds the snippet above.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.