Using Rules to Update Content

Events happening in the community are now at Drupal community events on www.drupal.org.
Anonymous's picture

I have users with 1 node of content type A and one of content type B - they can not create additional nopdes of these types.

When fields in node of content type A are updated, I need to update fields in the node of content type B automatically.

It looks like I can trigger an action in Rules when node A is saved, but could anyone suggest how I should copy the data to the required field in node B?

I've looked at the Reference, Node Reference and Entity Reference, but can not see how these will enable me to copy the fields.

Should I be looking at executing some PHP to access the target node through a views_get_view_results and then accessing and updating the fields?

How would I get Rules to execute some PHP code? I can't see an option for this.

Any pointers on how I can get the field data to copy over would be very much appreciated!

Many Thanks.

Comments

Similar Issue

j4's picture

Hi Oakridge,

Were you able to find a solution?

Thanks
Jaya

Yes - I found a solution

oakridge's picture

Jaya,

I found a solution to my problem with help from a training video from Nodeone, which you should be able to find at: http://nodeone.se/en/coding-for-rules-2

In the setup for the Rule I was able to specify an argument to pass, which I set as the node ID.

In my module I used views_get_view_result to identify the node I wanted to update and then accessed the node's variables.

foreach ($summaries as $summary) {

// Load up the summary node so that we can access its data.
$summnode = node_load($summary->nid);
$object = entity_metadata_wrapper('node', $snode);  
$nodetype = $object->type->value();
switch ($nodetype) 
{ 
    case '010_about':
  // Write the data into the summary fields
  $field_firstname = $object->field_firstname->value();
  $summnode->field_firstname[LANGUAGE_NONE][0]['value'] = $field_firstname;
  ...
  Other node types are handled in further case groups.

I found that I had to use the following code to update long text fields:

$field_long_text = $object->field_long_text->value();
$field_long_textv = $field_long_text['value'];
$summnode->field_long_text[LANGUAGE_NONE][0]['value'] = $field_long_textv;

I'd expect that someone would be able to do what I've coded in a more efficient way, but it works for me.

I hope that this helps - the Nodeone training is highly recommended!

Alan

Thanks for sharing

jcdub's picture

Your sample code got me on the right track!

Update: This tutorial also helped... http://pixeljets.com/blog/writing-robust-code-uses-fields-drupal-7

$object = entity_metadata_wrapper('node',$nid_to_work_on);
$object->$your_field_id = "new content here";
$object->save(); // <<< the key piece I was missing!

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: