I believe I have come across a which at first is quite hard to tell.
When using node_save on a node with multigroup fields, it seems each field does not properly keep its delta's.
Everything works fine when the form is being submitted but on trying to programmatically update the a node things get jumbled up and this from what i have seen only seems to happen to the last field in the group.

To begin with I thought I was doing something wrong so i got a clean d.6.14 with the latest cck3 and devel.
I created a node with 5 fields and added them multigroup field then had devel generate some example content.
I then executed the following piece of code sadly the same thing seems to happen.

$node= node_load(42);
//set the delta key using field_type+1 since field type is always used 
$delta_id = end(array_keys($node->field_type))+1;

$node->field_name[$delta_id][value] =  'flk';
$node->field_number[$delta_id][value] =  '124';
$node->field_type[$delta_id][value] =  'office';
$node->field_email[$delta_id][value] =  'flk@rocks';
$node->field_address[$delta_id][value] =  'middle of nowhere drive';
$node->field_note[$delta_id][value] = 'flk note here';


If the first few field of the last field (in this case note) is empty it moves any of the above note fields down instead of keeping each note with its associated delta.

I provided before & after pictures of the forms and 'views' as well as cck definition of node type in question for anyone who wants to test this out.

Members fund testing for the Drupal project. Drupal Association Learn more


flk’s picture

Component: content_multigroup.module » content.module
711 bytes

After following how the data is saved I can confirm this is not a content_multigroup.module issue but rather content.module issue.
The problem occurs in the content_set_empty() function which does not make use of the already provided delta's but rather recreates the items starting a new array.
Which is fine when a form is being submitted since items will contain fields that are not empty but when a node is being 'updated' via node_save we are just making use of the values retrieved from the db where empty fields were not saved to begin with.
So we if we 5 fields and 2 of them are empty node_load will provide us with 3 fields and there delta's. We need to keep hold of those delta's when saving the data so that we know which row of data it is associated with.

I attach a patch.

flk’s picture

Status: Active » Needs review
EHA’s picture

subscribing. will check out the patch.

markus_petrux’s picture