diff --git a/feeds.api.php b/feeds.api.php index 9aee118..b40c310 100644 --- a/feeds.api.php +++ b/feeds.api.php @@ -459,10 +459,6 @@ function my_module_mapper_unique(FeedsSource $source, $entity_type, $bundle, $ta /** * Example of the preprocess_callbacks specified in hook_feeds_processor_targets(). * - * @param FeedsSource $source - * The Feed source. - * @param object $entity - * The entity being processed. * @param array $target * The full target definition. * @param array &$mapping @@ -470,7 +466,7 @@ function my_module_mapper_unique(FeedsSource $source, $entity_type, $bundle, $ta * * @see hook_feeds_processor_targets() */ -function my_module_preprocess_callback(FeedsSource $source, $entity, array $target, array &$mapping) { +function my_module_preprocess_callback(array $target, array &$mapping) { // Add in default values. $mapping += array('setting_value' => TRUE); } diff --git a/mappers/locale.inc b/mappers/locale.inc index f1aef3a..5f235a1 100644 --- a/mappers/locale.inc +++ b/mappers/locale.inc @@ -19,7 +19,7 @@ function locale_feeds_processor_targets_alter(array &$targets, $entity_type, $bu /** * Preprocess callback that sets the configured mapping language. */ -function locale_feeds_preprocess_callback(FeedsSource $source, $target_item, array $target, array &$mapping) { +function locale_feeds_preprocess_callback(array $target, array &$mapping) { if (empty($mapping['field_language'])) { return; } diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc index 4ed85aa..d7102dc 100644 --- a/plugins/FeedsProcessor.inc +++ b/plugins/FeedsProcessor.inc @@ -706,7 +706,7 @@ abstract class FeedsProcessor extends FeedsPlugin { // 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) { + foreach ($this->getMappings() as $mapping) { if (isset($targets[$mapping['target']]['real_target'])) { $target_name = $targets[$mapping['target']]['real_target']; } @@ -714,20 +714,12 @@ abstract class FeedsProcessor extends FeedsPlugin { $target_name = $mapping['target']; } - // If the target is a field and a mapping language is specified, empty the - // value for the targeted language only. + // If the target is a field empty the value for the targeted language + // only. // In all other cases, just empty the target completely. - // @todo The definitive language to use is decided later, in - // ::mapToTarget(). In theory values for the wrong language could be - // emptied. - if (isset($mapping['field_language']) && isset($fields[$target_name])) { - // First check if the specified language is available. - $languages = language_list('enabled'); - if (!isset($languages[1][$mapping['field_language']])) { - $mapping['field_language'] = LANGUAGE_NONE; - } + if (isset($fields[$target_name])) { // Empty the target for the specified language. - $target_item->{$target_name}[$mapping['field_language']] = array(); + $target_item->{$target_name}[$mapping['language']] = array(); } else { // Empty the whole target. @@ -739,7 +731,7 @@ abstract class FeedsProcessor extends FeedsPlugin { // the parser's getSourceElement() method to retrieve the value of the // source element and pass it to the processor's setTargetElement() to stick // it on the right place of the target item. - foreach ($this->config['mappings'] as $mapping) { + foreach ($this->getMappings() as $mapping) { $value = $this->getSourceValue($source, $result, $mapping['source']); $this->mapToTarget($source, $mapping['target'], $target_item, $value, $mapping); @@ -786,24 +778,6 @@ abstract class FeedsProcessor extends FeedsPlugin { protected function mapToTarget(FeedsSource $source, $target, &$target_item, $value, array $mapping) { $targets = $this->getCachedTargets(); - if (isset($targets[$target]['preprocess_callbacks'])) { - foreach ($targets[$target]['preprocess_callbacks'] as $callback) { - call_user_func_array($callback, array($source, $target_item, $targets[$target], &$mapping)); - } - } - - // Ensure there's always a language set. - if (empty($mapping['language'])) { - $mapping['language'] = LANGUAGE_NONE; - } - else { - // Check if the configured language is available. If not, fallback to LANGUAGE_NONE. - $languages = language_list('enabled'); - if (!isset($languages[1][$mapping['language']])) { - $mapping['language'] = LANGUAGE_NONE; - } - } - // Map the source element's value to the target. // If the mapping specifies a callback method, use the callback instead of // setTargetElement(). @@ -947,7 +921,38 @@ abstract class FeedsProcessor extends FeedsPlugin { * Get mappings. */ public function getMappings() { - return isset($this->config['mappings']) ? $this->config['mappings'] : array(); + $cache = &drupal_static('FeedsProcessor::getMappings', array()); + + if (!isset($cache[$this->id])) { + $mappings = $this->config['mappings']; + $targets = $this->getCachedTargets(); + $languages = language_list('enabled'); + + foreach ($mappings as &$mapping) { + + if (isset($targets[$mapping['target']]['preprocess_callbacks'])) { + foreach ($targets[$mapping['target']]['preprocess_callbacks'] as $callback) { + call_user_func_array($callback, array($targets[$mapping['target']], &$mapping)); + } + } + + // Ensure there's always a language set. + if (empty($mapping['language'])) { + $mapping['language'] = LANGUAGE_NONE; + } + else { + // Check if the configured language is available. If not, fallback to + // LANGUAGE_NONE. + if (!isset($languages[1][$mapping['language']])) { + $mapping['language'] = LANGUAGE_NONE; + } + } + } + + $cache[$this->id] = $mappings; + } + + return $cache[$this->id]; } /** @@ -1088,7 +1093,7 @@ abstract class FeedsProcessor extends FeedsPlugin { protected function uniqueTargets(FeedsSource $source, FeedsParserResult $result) { $parser = feeds_importer($this->id)->parser; $targets = array(); - foreach ($this->config['mappings'] as $mapping) { + foreach ($this->getMappings() as $mapping) { if (!empty($mapping['unique'])) { // Invoke the parser's getSourceElement to retrieve the value for this // mapping's source. @@ -1152,7 +1157,7 @@ abstract class FeedsProcessor extends FeedsPlugin { protected function hash($item) { $sources = feeds_importer($this->id)->parser->getMappingSourceList(); $mapped_item = array_intersect_key($item, array_flip($sources)); - return hash('md5', serialize($mapped_item) . serialize($this->config['mappings'])); + return hash('md5', serialize($mapped_item) . serialize($this->getMappings())); } /** diff --git a/tests/feeds_tests.module b/tests/feeds_tests.module index 44fa2e8..2eea4d7 100644 --- a/tests/feeds_tests.module +++ b/tests/feeds_tests.module @@ -209,7 +209,7 @@ function feeds_tests_feeds_processor_targets_alter(array &$targets, $entity_type * * @see feeds_tests_feeds_processor_targets() */ -function feeds_tests_preprocess_callback(FeedsSource $source, $target_item, $target, array &$mapping) { +function feeds_tests_preprocess_callback(array $target, array &$mapping) { $mapping['required_value'] = TRUE; }