diff --git a/mappers/file.inc b/mappers/file.inc index 41b6c31..7637838 100644 --- a/mappers/file.inc +++ b/mappers/file.inc @@ -24,7 +24,6 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'real_target' => $name, 'allow_empty' => $allow_empty, 'form_callback' => 'feeds_ui_mapping_settings_allow_empty_form', - 'summary_callback' => 'feeds_ui_mapping_settings_allow_empty_summary', ); if ($info['type'] == 'image') { @@ -33,13 +32,17 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'callback' => 'file_feeds_set_target', 'description' => t('The alt tag of the @label field.', array('@label' => $instance['label'])), 'real_target' => $name, + 'allow_empty' => $allow_empty, + 'form_callback' => 'feeds_ui_mapping_settings_allow_empty_form', ); $targets[$name . ':title'] = array( 'name' => t('@label: Title', array('@label' => $instance['label'])), 'callback' => 'file_feeds_set_target', 'description' => t('The title of the @label field.', array('@label' => $instance['label'])), 'real_target' => $name, - ); + 'allow_empty' => $allow_empty, + 'form_callback' => 'feeds_ui_mapping_settings_allow_empty_form', + ); } } } @@ -52,20 +55,20 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function file_feeds_set_target($source, $entity, $target, $value, $mapping) { - if( empty($value) && !empty($mapping['allow_empty'])) { - //See if we already have a value for this, so we can - //allow empty, but keep the original value. - $node = node_load($entity->nid, $entity->vid); - $field = field_get_items('node', $node, preg_replace('/:uri/', '', $target)); - $field_value = $field[0]; - if (!empty($field_value)) { - $value = $field_value['uri']; +function file_feeds_set_target($source, $entity, $target, $value, $mapping, $target_item_copy) { + if (empty($value) && empty($mapping['allow_empty'])) { + //Empty values are ignored, so ignore our empty value and + //if existsing use the current value. + if (isset($target_item_copy)) { + $value = array(); + foreach ($target_item_copy[LANGUAGE_NONE] as $key => $val) { + $value[] = $val['uri']; + } } } elseif (empty($value) && empty($mapping['allow_empty'])) { return; } - + // Make sure $value is an array of objects of type FeedsEnclosure. if (!is_array($value)) { $value = array($value); @@ -76,7 +79,6 @@ function file_feeds_set_target($source, $entity, $target, $value, $mapping) { $info = field_info_field($field_name); if ($sub_field == 'uri') { - foreach ($value as $k => $v) { if (!($v instanceof FeedsEnclosure)) { if (is_string($v)) { @@ -87,13 +89,13 @@ function file_feeds_set_target($source, $entity, $target, $value, $mapping) { } } } + if (empty($value)) { return; } - + static $destination; if (!$destination) { - $entity_type = $source->importer->processor->entityType(); $bundle = $source->importer->processor->bundle(); $instance_info = field_info_instance($entity_type, $field_name, $bundle); diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc index c2b0af5..03af735 100755 --- a/plugins/FeedsProcessor.inc +++ b/plugins/FeedsProcessor.inc @@ -464,11 +464,19 @@ abstract class FeedsProcessor extends FeedsPlugin { if (empty($target_item)) { $target_item = array(); } - + // Many mappers add to existing fields rather than replacing them. Hence we // need to clear target elements of each item before mapping in case we are // mapping on a prepopulated item such as an existing node. + // But we do store the existing fields in a array, so if needed we can use them. + $target_item_copys = array(); foreach ($this->config['mappings'] as $mapping) { + //Make a copy of the current target item + //so if needed we can use it in oure mapper. + if (isset($target_item->{$targets[$this->id][$mapping['target']]['real_target']})) { + $target_item_copys[$targets[$this->id][$mapping['target']]['real_target']] = $target_item->{$targets[$this->id][$mapping['target']]['real_target']}; + } + // Unset existing fields. if (isset($targets[$this->id][$mapping['target']]['real_target'])) { unset($target_item->{$targets[$this->id][$mapping['target']]['real_target']}); } @@ -476,7 +484,7 @@ abstract class FeedsProcessor extends FeedsPlugin { unset($target_item->{$mapping['target']}); } } - + /* This is where the actual mapping happens: For every mapping we envoke the parser's getSourceElement() method to retrieve the value of the source @@ -499,19 +507,20 @@ abstract class FeedsProcessor extends FeedsPlugin { else { $value = $parser->getSourceElement($source, $result, $mapping['source']); } - + // Map the source element's value to the target. if (isset($targets[$this->id][$mapping['target']]) && is_array($targets[$this->id][$mapping['target']]) && isset($targets[$this->id][$mapping['target']]['callback']) && function_exists($targets[$this->id][$mapping['target']]['callback'])) { $callback = $targets[$this->id][$mapping['target']]['callback']; - $callback($source, $target_item, $mapping['target'], $value, $mapping); + $callback($source, $target_item, $mapping['target'], $value, $mapping, $target_item_copys[$targets[$this->id][$mapping['target']]['real_target']]); } else { $this->setTargetElement($source, $target_item, $mapping['target'], $value, $mapping); } } + return $target_item; } @@ -648,7 +657,7 @@ abstract class FeedsProcessor extends FeedsPlugin { * * @ingroup mappingapi */ - public function setTargetElement(FeedsSource $source, $target_item, $target_element, $value) { + public function setTargetElement(FeedsSource $source, $target_item, $target_element, $value, $target_item_copy) { switch ($target_element) { case 'url': case 'guid':