0

I'm searching a module to clone several nodes automatically when i create a project node type. To explain better : When i create a new node of Project type for exemple, i need to clone some nodes and tie this nodes to my node project.

I have tried node_clone but we should use a button to clone the node. For the moment i developped a small code to clone my node like this :

$p1template=variable_get('myvar_p1');
$p1=node_load($p1template);
unset($p1->nid);
unset($p1->vid);
$p1->title='Processus P1'.$projectid;
$p1->field_px_projet['und'][0]['target_id']=$projectid;
node_save($p1);

But i had a second problem with the field_collection. when i cloned the nodes, the field_collection keep the same reference and when i modify a field collection, all the nodes cloned field_collection was modify...

How can i fix this problem ?

PS : Sorry for my english..

Thx

0

Node clone module has a hook for this, hook_clone_node_alter(&$node, $context). When you clone nodes programmatically, make sure you alter their state and values inside this function.

function module_name_clone_alter(&$node, $context){
    switch ($context['method']) {
        case 'prepopulate':
          // Change field values ....

          break;

        case 'save-edit':
          // Perform operations when saving the node.

          break;
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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