diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc index 49404c2..43ef0c1 100644 --- a/plugins/FeedsProcessor.inc +++ b/plugins/FeedsProcessor.inc @@ -363,18 +363,6 @@ abstract class FeedsProcessor extends FeedsPlugin { $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. - foreach ($this->config['mappings'] as $mapping) { - if (isset($targets[$mapping['target']]['real_target'])) { - unset($target_item->{$targets[$mapping['target']]['real_target']}); - } - elseif (isset($target_item->{$mapping['target']})) { - 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 @@ -398,16 +386,29 @@ abstract class FeedsProcessor extends FeedsPlugin { $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); - } - else { - $this->setTargetElement($source, $target_item, $mapping['target'], $value); + if (!empty($value)) { + // 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 this only for provided mappers, so $value was checked first. + if (isset($targets[$mapping['target']]['real_target'])) { + unset($target_item->{$targets[$mapping['target']]['real_target']}); + } + elseif (isset($target_item->{$mapping['target']})) { + unset($target_item->{$mapping['target']}); + } + + // 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); + } + else { + $this->setTargetElement($source, $target_item, $mapping['target'], $value); + } } } return $target_item;