diff -u b/field_collection.module b/field_collection.module --- b/field_collection.module +++ b/field_collection.module @@ -1910,10 +1910,6 @@ $delta = 0; $field = isset($entity->$target) ? $entity->$target : array(); foreach ($value as $v) { - if (empty($v)) { - // Avoid creation of empty field collections - continue; - } if (isset($field['und'][$delta]['entity'])) { $field_collection_item = $field['und'][$delta]['entity']; } @@ -1929,7 +1925,6 @@ $callback($source, $field_collection_item, $sub_target, $v); } - $field['und'][$delta]['entity'] = $field_collection_item; $field['und'][$delta]['value'] = $field_collection_item->item_id; @@ -1945,0 +1941,40 @@ + +/** + * Implements hook_feeds_presave(). + */ +function field_collection_feeds_presave($source, $entity) { + // Do not save any empty field collection items that may have been created + // during the mapping process. Since the mapping is done field by field in + // field_collection_feeds_set_target(), we have to wait until this hook (when + // the field collection item is fully built up) before we can check if it's + // empty. + $config = $source->importer()->getConfig(); + if (!empty($config['processor']['config']['mappings'])) { + // Find all field collection mappings. + foreach ($config['processor']['config']['mappings'] as $mapping) { + if (isset($mapping['target'])) { + $args = explode(':', $mapping['target']); + $field_name = array_shift($args); + if (($field = field_info_field($field_name)) && $field['type'] == 'field_collection') { + // If the field collection item is empty, do not save it. + if (!empty($entity->{$field_name})) { + foreach ($entity->{$field_name} as $langcode => &$items) { + foreach ($items as $delta => $item) { + if (isset($item['entity']) && field_collection_item_is_empty($item['entity'])) { + unset($items[$delta]); + } + } + // Clean up the final array. + if (empty($items)) { + unset($entity->{$field_name}[$langcode]); + } + else { + $items = array_values($items); + } + } + } + } + } + } + } +}