diff --git a/field_collection.module b/field_collection.module index aca6721..e3f1a06 100644 --- a/field_collection.module +++ b/field_collection.module @@ -2096,11 +2096,7 @@ function field_collection_feeds_set_target($source, $entity, $target, $value, $m $delta++; } - - // If new field value has less field collections than old one, remove extra collections. - if (count($value) < count($field['und'])) { - $field['und'] = array_slice($field['und'], 0, count($value)); - } + } catch (Exception $e) { drupal_set_message($e->getMessage(), 'error'); @@ -2176,3 +2172,44 @@ function field_collection_file_feeds_set_target($source, $entity, $target, $valu $entity->{$target} = $field; } + + +/** + * Invokes hook_feeds_presave(). + * Invoked before a feed item is saved. + * + * @param FeedsSource $source + * FeedsSource object that describes the source that is being imported. + * @param $entity + * The entity object. + * @param array $item + * The parser result for this entity. + * @param int|null $entity_id + * The id of the current item which is going to be updated. If this is a new + * item, then NULL is passed. + */ +function field_collection_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) { + + // Only hook in when updating the entity. + if (!is_null($entity_id)) { + $fc_fields = array(); + + // First, figure out which fields are field collections. + $fields = field_info_instances($entity->feeds_item->entity_type, $entity->type); + foreach ($fields as $field_name => $field) { + if ($field['widget']['module'] == 'field_collection') { + $fc_fields[] = $field_name; + } + } + + // Check currently stored entity for field collections. + $old_entities = entity_load($entity->feeds_item->entity_type, array($entity_id)); + if (!empty($old_entities)) { + $old_entity = array_shift($old_entities); + foreach ($fc_fields as $field_name) { + // @todo: Find a way to distinguish which field collections to delete, which to keep. + } + } + + } +}