You are here

function feeds_tamper_feeds_after_parse in Feeds Tamper 7

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

Implements hook_feeds_after_parse().

This is the meat of the whole deal. After every Feeds run, before going into processing, this gets called and modifies the feed items.

@todo Add support for source plugins.

File

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

Code

function feeds_tamper_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  $importer_instances = feeds_tamper_load_by_importer($source->importer->id, FALSE);

  // Don't go through all of the nonsense if we don't have anything to do.
  if (empty($importer_instances)) {
    return;
  }
  $parser = $source->importer->parser;
  $is_csv = $parser instanceof FeedsCSVParser;
  $sources = array_fill_keys(feeds_tamper_get_unique_source_list($source->importer), '');
  $plugins = feeds_tamper_get_plugins();
  foreach (array_keys($result->items) as $item_key) {

    // Initialize every source value.
    $result->items[$item_key] += $sources;
    foreach ($importer_instances as $element_key => $instances) {

      // Break if the previous element's plugins removed the item, otherwise the
      // value will be re-populated.
      if (!isset($result->items[$item_key])) {
        break;
      }
      if ($is_csv) {
        $element_key = drupal_strtolower($element_key);
      }

      // Manually advance the current_item key since we can't use shiftItem().
      // Plugins can change it, so re-set.
      $result->current_item = $result->items[$item_key];

      // Plugins assume that everything lives in the item array.
      $result->items[$item_key][$element_key] = $parser
        ->getSourceElement($source, $result, $element_key);
      foreach ($instances as $instance) {

        // If the item was unset by previous plugin, jump ahead.
        if (!isset($result->items[$item_key])) {
          break 2;
        }

        // Array-ness can change depending on what the plugin is doing.
        $is_array = is_array($result->items[$item_key][$element_key]);
        $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);
        }
      }
    }
  }
  $result->current_item = NULL;
}