Problem/Motivation
I have a situation that is apparently rather unique. I have a multivalue relation select field, which references a relation that has a single field collection on it. I also have a couple of field collection hook implementations that work on those field collection items as they're saved. When the first relation of the field is added and the node is saved, all the hooks fire as expected. If editing the node and adding a second or third relation to the same multivalue relation select field, the field collection hooks do not fire.
Where the change is happening is in relation_select_field_insert(). When saving the node after adding the first relation, the first main condition is met if (empty($relations)) {. In it, there is this loop:
foreach ($item['relation_fields'] as $relation_field_name => $relation_field) {
$relation->{$relation_field_name} = $relation_field;
}
followed immediately by a relation_save($relation);
When saving the node after adding a second or third relation, the 2nd condition is met: elseif (!empty($relations)) {. In it, there is this loop:
foreach ($item['relation_fields'] as $relation_field_name => $relation_field) {
if (isset($relation_fields[$relation_field_name])) {
// Update an existing relation field instance.
$langcode = array_shift(array_keys($relation_field));
$relation_field_items = array_shift($relation_field);
$relation_field = field_info_field($relation_field_name);
field_default_submit('relation', $relation, $relation_field, $relation_fields[$relation_field_name], $langcode, $relation_field_items, $item, $form_state);
$relation->{$relation_field_name}[$langcode] = $relation_field_items;
}
else {
// Create a new relation field instance.
$relation->{$relation_field_name} = $relation_field;
}
}
It does essentially the same thing as the first loop, but it looks like it's being more careful to update only the correct $langcode.
The cause of the problem I experienced was the field_default_submit() call, which filters out empty values from $relation_field_items. In the first condition, this function is not called, allowing empty values to be passed, thus triggering the hooks I've come to expect.
Proposed resolution
One solution is to remove the field_default_submit() call, so that empty values still pass through, as in the first condition. I think this would still allow expected behavior, but I don't know why it was there to begin with, or why it's called in the second condition and not in the first.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | problems_adding_items-2575169-2.patch | 1.11 KB | nadavoid |
Comments
Comment #2
nadavoid commentedComment #3
nadavoid commentedgrammar correction.
Comment #5
nadavoid commentedAdded patch in commit b3ed450d920ace4fef4b2078cd38142dbb04e6e8.