I have a rule which is triggered when a node is flagged. Currently, the rule loads some content (1) that has been referenced by a field on the flagged content. I have set the rule to create a new piece of content (2) and would like a node reference field on this new piece of content to reference content (1). I know there is a way to do this with PHP but am not sure of it...

link|improve this question
feedback

1 Answer

This should actually be pretty simple:

  1. Create your rule for the event "A node has been flagged, under "NAME OF YOUR FLAG" and name your rule
  2. Add an action of "Add new content" and click "Next"
  3. Choose what you want the auto-created content's title to be (w/ or w/out tokens) and select what content type you want created and click "Save"
  4. Add another action of "Populate a field", for the arguments configurations choose "new content type of CONTENT TYPE YOU CREATED" and for field, choose your node reference field and click "Continue"
  5. Again on arguments change to "new content type of CONTENT TYPE YOU CREATED"
  6. Open the fieldset titled: "Advanced: Specify the fields value with PHP code" and enter:

return array(0 => array('nid' => $node->field_NAMEOFYOURFIELD[0]['nid']));

Note: You can find the field names by going to admin/content/types and click "Manage Fields" on the content type you want OR you can do a print_r($node); on a node of that content type to see the node object structure

Now when you view your rule, you should see something like:

Rule Example

and whenever you use that flag, a new node will be created having the same reference as the node you flagged.

link|improve this answer
Hi, thank you so much for your help. Embarrassingly, I can't remember how to find out a field's name in terms of how to reference it with PHP...can you remind me? – Drupal Reggie Feb 23 at 17:17
Well you can find the field names by going to admin/content/types and click "Manage Fields" on the content type you want OR you can do a print_r($node); on a node of that content type to see the node object structure – Laxman13 Feb 23 at 17:19
Awesome! Thank you! – Drupal Reggie Feb 23 at 17:24
Another question if I may...I'm now trying to get rules to create a new piece of content after a node has been flagged. There's a node reference field on the new content and I'd like it to reference the flagged node if possible...more PHP i assume? – Drupal Reggie Feb 23 at 19:46
Follow the same steps as above except in 4-5 for the argument, used "flagged node" instead – Laxman13 Feb 23 at 19:55
feedback

Your Answer

 
or
required, but never shown

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