You are here

function feeds_tamper_feeds_after_parse in Feeds Tamper 6

Same name and namespace in other branches
  1. 7 feeds_tamper.module \feeds_tamper_feeds_after_parse()

Implementation of hook_feeds_after_parse().

After every Feeds import, before going into processing, this gets called and modifies the data based on the configuration.

File

./feeds_tamper.module, line 19
Feeds Tamper - basic API functions and hook implementations.

Code

function feeds_tamper_feeds_after_parse(FeedsImporter $importer, FeedsSource $source) {
  $importer_instances = feeds_tamper_load_by_importer($source->importer->id, FALSE);
  if (empty($importer_instances)) {
    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;
    }
    elseif ($csv) {
      unset($importer_instances[$element_key]);
      $importer_instances[drupal_strtolower($element_key)] = $instance;
    }
  }
  $plugins = feeds_tamper_get_plugins();
  foreach ($source->batch->items as $item_key => &$item) {

    // Add the blank sources to the item.
    foreach ($blank_mappings as $blank) {
      $item[$blank] = '';
    }
    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($source->batch->items[$item_key])) {
            break 2;
          }
          $plugin = $plugins[$instance->plugin_id];
          $is_array = is_array($item[$element_key]);
          if ($is_array && $plugin['multi'] == 'loop') {
            foreach ($item[$element_key] as &$i) {
              $plugin['callback']($source, $item_key, $element_key, $i, $instance->settings);
            }
          }
          elseif ($is_array && $plugin['multi'] == 'direct') {
            $plugin['callback']($source, $item_key, $element_key, $item[$element_key], $instance->settings);
          }
          elseif (!$is_array && $plugin['single'] != 'skip') {
            $plugin['callback']($source, $item_key, $element_key, $item[$element_key], $instance->settings);
          }
        }
      }
    }
  }
}