1

I' trying to create a rule component that clones selected nodes and then changes some fields, using Rules, VBO and Node Clone.

VBO and Node clone work fine, but I can't change values of every field in the clones afterwards. After the action "Clone node", I can add a "set data to value" action to change just core node fields (like status, sticky...), but the selector doesn't show me my other fields defined in the content type (like cloned-node:field_xyz).

I also tried to use Rules Bonus Pack instead of Node clone to duplicate nodes, but with no luck: same issue.

After some researches, I tried to force a save entity action on the cloned node and then tried to load entity from cloned-nid, but I cannot choose my content type's fields to change them.

I also added a condition on top like "Content is type | entity has a field | entity is bundle", but nothing happens.

Probably I miss something. Any idea?

Thanx in advance

2 Answers 2

0

I solved using custom PHP code in my component instead of the node clone action, so I'd like to share this with you:

// load the original node
$node = node_load($node_parameter->nid);
$node->nid = null;
$node->vid = null;
// park the new node
node_object_prepare($node);
// reset fields
$node->uid = 1;
$node->status = 1;
$node->path = null;
$node->created = 1432072800;
$node->title = $site->current_date;
$node->field_one['und'][0]['target_id']= 11530;
$node->field_two['und'][0]['value']= 'my text';
unset($node->field_three);
// save the new node
node_save($node);
// flush
unset($node);  

Probably the key is that when you use the action "node clone", Rules alone can't determine the cloned node type and access to its fields directly, even if you are cloning a node already verified by the condition at the beginning of the component. Probably it could if putting another condition after the clone action, but I don't know how to.

So another solution I tried is to create a component that just clones the VBO passed node. And then create a separated rule that reacts on event "after cloning a node" and put in there the condition "content is type of" and all the "set data value" actions I needed.

And it works.

Anyway I prefer the first solution, so I can keep all I need in just one place and avoid using Node Clone module. I was worried about performance executing PHP and node_load() in Rules, but in just few minutes I successfully cloned and updated 2260 nodes.

Thanx all

0

@Cheope hit on the problem exactly:

Probably the key is that when you use the action "node clone", Rules alone can't determine the cloned node type and access to its fields directly, even if you are cloning a node already verified by the condition at the beginning of the component. Probably it could if putting another condition after the clone action, but I don't know how to.

You can do that without writing custom code, using the Conditional Rules module. This module lets you intersperse conditionals into the actions section of a rule.

After the node clone action, you can add a conditional to check the content type of the created node. When you add actions inside of the conditional, they can access the content-type-specific fields of the created node.

Your Answer

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

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