diff --git a/mappers/file.inc b/mappers/file.inc index 45c56c3..12ee765 100644 --- a/mappers/file.inc +++ b/mappers/file.inc @@ -49,11 +49,7 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * element the user has picked as a source. */ function file_feeds_set_target($source, $entity, $target, $value) { - if (empty($value)) { - return; - } - - // Make sure $value is an array of objects of type FeedsEnclosure. + // Make sure $value is an array. if (!is_array($value)) { $value = array($value); } @@ -63,20 +59,16 @@ function file_feeds_set_target($source, $entity, $target, $value) { $info = field_info_field($field_name); if ($sub_field == 'uri') { - foreach ($value as $k => $v) { if (!($v instanceof FeedsEnclosure)) { if (is_string($v)) { $value[$k] = new FeedsEnclosure($v, file_get_mimetype($v)); } else { - unset($value[$k]); + $value[$k] = NULL; } } } - if (empty($value)) { - return; - } static $destination; if (!$destination) { @@ -97,11 +89,20 @@ function file_feeds_set_target($source, $entity, $target, $value) { // Populate entity. $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array()); + + // Allow for multiple mappings to the same target but also allow to add other + // sub-fields. Delta will have the value of the first item, where subfield is + // not present. $delta = 0; + while (isset($field[LANGUAGE_NONE][$delta][$sub_field])) { + $delta++; + } + foreach ($value as $v) { - if ($info['cardinality'] == $delta) { - break; - } + // Due to the reason that empty values for sub-fields are allowed which may + // result in invalid items which will be sorted out later in + // file_feeds_presave(), the fields cardinality is not generally checked + // here, but it will be done in case of the uri sub-field. if (!isset($field[LANGUAGE_NONE][$delta])) { $field[LANGUAGE_NONE][$delta] = array(); @@ -112,8 +113,26 @@ function file_feeds_set_target($source, $entity, $target, $value) { case 'title': $field[LANGUAGE_NONE][$delta][$sub_field] = $v; break; - case 'uri': + // Do not add more files than defined in fields cardinality. + if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { + $fids_count = 0; + foreach ($field[LANGUAGE_NONE] as $check_item) { + if (isset($check_item['fid'])) { + // TODO: It would be even better to not count duplicate fids. + $fids_count++; + } + // If fields cardinality has been reached, do not add more files to + // this field. + if ($info['cardinality'] == $fids_count) { + $entity->$field_name = $field; + return; + } + } + } + // Mark, that for current delta sub-field was processed, to not mess up + // with other sub-fields in case of failure. + $field[LANGUAGE_NONE][$delta]['uri'] = 1; try { $file = $v->getFile($destination); $field[LANGUAGE_NONE][$delta] += (array) $file;