You are here

protected function FeedsProcessor::map in Feeds 6

Same name and namespace in other branches
  1. 7.2 plugins/FeedsProcessor.inc \FeedsProcessor::map()
  2. 7 plugins/FeedsProcessor.inc \FeedsProcessor::map()

Execute mapping on an item.

This method encapsulates the central mapping functionality. When an item is processed, it is passed through map() where the properties of $source_item are mapped onto $target_item following the processor's mapping configuration.

For each mapping FeedsParser::getSourceElement() is executed to retrieve the source element, then FeedsProcessor::setTargetElement() is invoked to populate the target item properly. Alternatively a hook_x_targets_alter() may have specified a callback for a mapping target in which case the callback is asked to populate the target item instead of FeedsProcessor::setTargetElement().

See also

hook_feeds_parser_sources_alter()

hook_feeds_data_processor_targets_alter()

hook_feeds_node_processor_targets_alter()

hook_feeds_term_processor_targets_alter()

hook_feeds_user_processor_targets_alter()

Related topics

5 calls to FeedsProcessor::map()
FeedsDataProcessor::process in plugins/FeedsDataProcessor.inc
Implementation of FeedsProcessor::process().
FeedsFeedNodeProcessor::map in plugins/FeedsFeedNodeProcessor.inc
Execute mapping on an item.
FeedsNodeProcessor::process in plugins/FeedsNodeProcessor.inc
Implementation of FeedsProcessor::process().
FeedsTermProcessor::map in plugins/FeedsTermProcessor.inc
Execute mapping on an item.
FeedsUserProcessor::map in plugins/FeedsUserProcessor.inc
Execute mapping on an item.
3 methods override FeedsProcessor::map()
FeedsFeedNodeProcessor::map in plugins/FeedsFeedNodeProcessor.inc
Execute mapping on an item.
FeedsTermProcessor::map in plugins/FeedsTermProcessor.inc
Execute mapping on an item.
FeedsUserProcessor::map in plugins/FeedsUserProcessor.inc
Execute mapping on an item.

File

plugins/FeedsProcessor.inc, line 84

Class

FeedsProcessor
Abstract class, defines interface for processors.

Code

protected function map(FeedsImportBatch $batch, $target_item = NULL) {

  // Static cache $targets as getMappingTargets() may be an expensive method.
  static $sources;
  if (!isset($sources[$this->id])) {
    $sources[$this->id] = feeds_importer($this->id)->parser
      ->getMappingSources();
  }
  static $targets;
  if (!isset($targets[$this->id])) {
    $targets[$this->id] = $this
      ->getMappingTargets();
  }
  $parser = feeds_importer($this->id)->parser;
  if (empty($target_item)) {
    $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.
  $convert_to_array = FALSE;
  if (is_array($target_item)) {
    $target_item = (object) $target_item;
    $convert_to_array = TRUE;
  }
  foreach ($this->config['mappings'] as $mapping) {
    if (isset($targets[$this->id][$mapping['target']]['real_target'])) {
      unset($target_item->{$targets[$this->id][$mapping['target']]['real_target']});
    }
    elseif (isset($target_item->{$mapping['target']})) {
      unset($target_item->{$mapping['target']});
    }
  }
  if ($convert_to_array) {
    $target_item = (array) $target_item;
  }

  /*
  This is where the actual mapping happens: For every mapping we envoke
  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.

  If the mapping specifies a callback method, use the callback instead of
  setTargetElement().
  */
  self::loadMappers();
  foreach ($this->config['mappings'] as $mapping) {

    // Retrieve source element's value from parser.
    if (isset($sources[$this->id][$mapping['source']]) && is_array($sources[$this->id][$mapping['source']]) && isset($sources[$this->id][$mapping['source']]['callback']) && function_exists($sources[$this->id][$mapping['source']]['callback'])) {
      $callback = $sources[$this->id][$mapping['source']]['callback'];
      $value = $callback($batch, $mapping['source']);
    }
    else {
      $value = $parser
        ->getSourceElement($batch, $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($target_item, $mapping['target'], $value);
    }
    else {
      $this
        ->setTargetElement($target_item, $mapping['target'], $value);
    }
  }
  return $target_item;
}