You are here

function field_collection_feeds_presave in Field collection 7

Implementation of hook_feeds_presave().

Invoked before a feed item is saved.

Parameters

FeedsSource $source: FeedsSource object that describes the source that is being imported.

$entity: The entity object.

array $item: The parser result for this entity.

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.

File

./field_collection.module, line 2477
Module implementing field collection field type.

Code

function field_collection_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
  $feed_config = $source
    ->importer()
    ->getConfig();
  $mappings = $feed_config['processor']['config']['mappings'];

  // Only hook in when updating the entity.
  if (empty($entity_id)) {

    // Go through $mappings and find the FC fields.
    foreach ($mappings as $mapping) {
      if (strpos($mapping['target'], ':') !== FALSE) {
        $parts = explode(':', $mapping['target']);
        $fc_name = array_shift($parts);
        $info = field_info_field($fc_name);

        // If the field is not a field collection, skip it.
        if ($info['type'] !== 'field_collection') {
          continue;
        }
        if (isset($entity->{$fc_name})) {
          foreach ($entity->{$fc_name}[LANGUAGE_NONE] as $delta => $fc_unloaded) {

            // If an FC is not loaded, we don't want it. Unset it so it won't save.
            if (!isset($entity->{$fc_name}[LANGUAGE_NONE][$delta]['entity'])) {
              unset($entity->{$fc_name}[LANGUAGE_NONE][$delta]);
            }
          }
        }
      }
    }
  }
}