tags:

views:

393

answers:

3

My question is about: "adding field data to referenced node without replacing existing data".

For example, I have a project node with team members referencing the project. Each team member has on its node a location, ie, 'United Kingdom', 'United States', 'Australia'.

On the project node I have those exact same fields. I need to create a rule so that when a 'team member' node is created, its location is added to the project node without replacing existing content.

So for instance, a project node with a team member from the United Kingdom would also have on its location field, 'United Kingdom'. When a team member from 'United States' is added, the project's location field would have 'United Kingdom' and 'United States'. When a team member who's location is both Canada AND France is added, the project's location becomes United Kingdom, United States, Canada, and France.

Thanks!

Doing something like:

return array(
  0 => array('value' => 'United Kingdom')
);

Just wouldn't work! It would replace the existing values. How do I make it so that it adds on to the existing values. Thanks!

A: 

Have you tried:

return array(
  array('value' => 'United Kingdom'),
  array('value' => 'United States'),
);
ceejayoz
That would add United Kingdom and United States, but that would replace existing Canada and France locations.
Jourkey
A: 

Will something like this work? Basically we capture the current nodes cck location field (change the field names below to fit), load the reference node, and add the location data to it, and save it away. I've not added the code to check to see if the location already exists, but that's something for another day. - Hope it helps.

#some debug data below
#krumo ($node);
#print "<pre>". print_r($node,true) . "</pre>";

#$node is our current data set

# save the current $node nid into a variable
$nid = $node->nid; 
#get the reference nid 
$refnid = $node->field_refnid[0][nid];
#get the location
$currentlocation = $node->field_team_location[0][value];

# nowload the reference node
$refnode = node_load ($refnid);
# some debug data below
#krumo ($refnode);
#print "<pre>". print_r($refnode,true) . "</pre>";

$newlocation = array ("value"=>$currentlocation);
$refnode->field_loacations[] = $newlocation;
#now save the reference node
node_save ($refnode);

#drupal_goto ("node/$nid");
Matt
Jourkey - I saw the tick, was this what you were looking for?
Matt
+1  A: 

Is it important to actually have the reference on the node or just to display the location.

If you are just worried about displaying the location then I think you can do this quite easily with a view.

There is a node reference reverse option I believe, but this would only display the team members and not the location.

If it is important to actually have the location information in the project node, then you will have to use hook_nodeapi op = save with code similar to Matts' answer.

Jeremy French