--- modules/feeds/mappers/file.inc 2013-11-12 17:23:54 +0000 +++ modules/feeds/mappers/file.inc 2013-12-10 09:38:19 +0000 @@ -130,6 +130,10 @@ $field[$langcode][$delta]['display'] = 1; } catch (Exception $e) { + // Something went wrong getting the file so this file field value is + // not valid. Due to a possible second call of this function to set + // the title or alt attributes the $field[LANGUAGE_NONE][$delta] won't + // be removed her. But there is a validation in file_feeds_presave(). watchdog_exception('Feeds', $e, nl2br(check_plain($e))); } break; @@ -140,3 +144,59 @@ $entity->$field_name = $field; } + +/** + * Implements hook_feeds_presave(). + */ +function file_feeds_presave(FeedsSource $source, $entity, $item) { + // Validation of all file/image fields is needed. Due to design of + // file_feeds_set_target() its not posible to do it there, so do it here. + list($id, $vid, $bundle) = entity_extract_ids($entity->feeds_item->entity_type, $entity); + $field_info_instances = field_info_instances($entity->feeds_item->entity_type, $bundle); + foreach ($field_info_instances as $name => $instance) { + $field_info = field_info_field($name); + if (in_array($field_info['type'], array('file', 'image'))) { + $provided = FALSE; + $languages = array(); + if (isset($entity->{$field_info['field_name']})) { + foreach ($entity->{$field_info['field_name']} as $langcode => $value) { + if(!empty($value)) { + $provided = TRUE; + $languages[] = $langcode; + } + } + // Nothing to check, if field is not provided + if (!$provided) { + continue; + } + + // Check each file field if fid is set, which isn't the case if adding the + // file fails in file_feeds_set_target(). + foreach ($languages as $langcode) { + foreach ($entity->{$field_info['field_name']}[$langcode] as $delta => $value) { + if (isset($value['fid'])) { + // fid is set, everything is fine. + continue; + } + + // If there is no fid probably something went wrong on getting the file. + // This could happen if i.e. the file title or alt attribute was set by + // file_feeds_set_target(), but then there was actually no file added + // (i.e. while file not found). + // A exception would have been already thrown before while adding the + // file, so just unset the uncomplete file field delta. + unset($entity->{$field_info['field_name']}[$langcode][$delta]); + if (empty($entity->{$field_info['field_name']}[$langcode])) { + // Unset the complete field if there are no more values. + unset($entity->{$field_info['field_name']}[$langcode]); + } + } + } + if (empty($entity->{$field_info['field_name']})) { + // Unset the complete field if there are no more values. + unset($entity->{$field_info['field_name']}); + } + } + } + } +}