Assign values to individual elements of multivalued field

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

How do I assign values to individual elements of a multivalued field with Rules? I know how to assign a value to a single valued field with the "Populate a field" action. This does not work for a multivalued field, as I want to assign a value to specific fields in the array.

I tried to use custom PHP code following the suggestion under the code entry box:

Expected format:
return array(
0 => array('nid' => value for nid),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-valued:
1 => array('nid' => value for nid),
2 => ...
);

So, when I want to assign values to element nr 4 and 9 I tried:

return array(
4 => array('nid' => 7),
9 => array('nid' => 3),
);

It appears that I always end up with only two populated elements, with index 0 and 1. So I lose the values that were already stored earlier in the elements other than 4 and 9. Who can give me some hints? Is this specific behaviour of PHP coding ?

regards,
Fred

Comments

Populate field: replaces previous value

Ashford's picture

Since Populate field means literally you want to populate the field, it would replace what's already there. You need to get the original values, so you can retain the unchanged values.

I am a Drupal beginner, so perhaps one of the Drupal experts will have a different solution. Try this on a Test site, not your live site. PHP code errors can generate blank pages for your entire site.

Drupal node 'field populate' array
Select option with multiple values
myArray(
0 => array('nid' => 4),
1 => array('nid' => 7),
2 => array('nid' => 3),
);

// Generate a new array list with the original keys and values
$newArray = array($node->field_your_fieldName[0]['value']);

// Loop through the list
foreach ($newArray as $key => $val)
{
  // Do we need to change the value for this key?
  if ($key == '4') {
      $val = array('nid' => 7);
  }
  if ($key == '9') {
      $val = array('nid' => 3);
  }


// Assign the new list to the field
return array($newArray);

}

Content Construction Kit (CCK)

Group organizers

Group notifications

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