diff --git a/feeds_tamper.module b/feeds_tamper.module index ecbc2bc..45334de 100644 --- a/feeds_tamper.module +++ b/feeds_tamper.module @@ -24,55 +24,45 @@ function feeds_tamper_feeds_after_parse(FeedsSource $source, FeedsParserResult $ return; } - $csv = FALSE; - if (get_class($source->importer->parser) == 'FeedsCSVParser') { - $csv = TRUE; - } - - // Look for any "Blank source" mappings as those will be treated specially. - $mappings = $source->importer->processor->config['mappings']; - $blank_mappings = array(); - - foreach ($importer_instances as $element_key => $instance) { - if (strpos($element_key, 'Blank source ') === 0) { - $blank_mappings[] = $element_key; - } - // Special case for FeedsCSVParser. - elseif ($csv) { - unset($importer_instances[$element_key]); - $importer_instances[drupal_strtolower($element_key)] = $instance; - } + $parser = $source->importer->parser; + $is_csv = FALSE; + if (get_class($parser) == 'FeedsCSVParser') { + $is_csv = TRUE; } $plugins = feeds_tamper_get_plugins(); - foreach ($result->items as $item_key => &$item) { - // Add the blank sources to the item. - foreach ($blank_mappings as $blank) { - $item[$blank] = ''; - } + foreach ($result->items as $item_key => $item) { + // Manually advance the current_item key since we can't use shiftItem(). + $result->current_item = $item; + foreach ($importer_instances as $element_key => $instances) { - if (isset($item[$element_key])) { - foreach ($instances as $instance) { - // If the item was unset by previous plugin, jump ahead. - if (!isset($result->items[$item_key])) { - break 2; - } + if ($is_csv) { + $element_key = drupal_strtolower($element_key); + } - $plugin = $plugins[$instance->plugin_id]; - $is_array = is_array($item[$element_key]); + // Plugins assume that everything lives in the item array. + $result->items[$item_key][$element_key] = $parser->getSourceElement($source, $result, $element_key); + $is_array = is_array($result->items[$item_key][$element_key]); - if ($is_array && $plugin['multi'] == 'loop') { - foreach ($item[$element_key] as &$i) { - $plugin['callback']($result, $item_key, $element_key, $i, $instance->settings, $source); - } - } - elseif ($is_array && $plugin['multi'] == 'direct') { - $plugin['callback']($result, $item_key, $element_key, $item[$element_key], $instance->settings, $source); - } - elseif (!$is_array && $plugin['single'] != 'skip') { - $plugin['callback']($result, $item_key, $element_key, $item[$element_key], $instance->settings, $source); + foreach ($instances as $instance) { + // If the item was unset by previous plugin, jump ahead. + if (!isset($result->items[$item_key])) { + break 2; + } + + $plugin = $plugins[$instance->plugin_id]; + + if ($is_array && $plugin['multi'] == 'loop') { + foreach ($result->items[$item_key][$element_key] as &$i) { + $plugin['callback']($result, $item_key, $element_key, $i, $instance->settings, $source); } } + elseif ($is_array && $plugin['multi'] == 'direct') { + $plugin['callback']($result, $item_key, $element_key, $result->items[$item_key][$element_key], $instance->settings, $source); + } + elseif (!$is_array && $plugin['single'] != 'skip') { + $plugin['callback']($result, $item_key, $element_key, $result->items[$item_key][$element_key], $instance->settings, $source); + } } } }