You are here

protected function FeedsAtomRDFProcessor::map in Feeds Atom 6

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

Override parent::map() to load all available add-on mappers.

We also add a $source parameter that contains the FeedsSource object that controls this feed.

Overrides FeedsProcessor::map

1 call to FeedsAtomRDFProcessor::map()
FeedsAtomRDFProcessor::process in plugins/FeedsAtomRDFProcessor.inc
Implementation of FeedsProcessor::process().

File

plugins/FeedsAtomRDFProcessor.inc, line 142
Contains the feeds atom RDF processor class.

Class

FeedsAtomRDFProcessor
Creates nodes from feed items.

Code

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

  // 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.
  if (is_array($target_item)) {
    $target_item = (object) $target_item;
    $convert_to_array = TRUE;
  }
  foreach ($this->config['mappings'] as $mapping) {
    if (isset($targets[$mapping['target']]['real_target'])) {
      unset($target_item->{$targets[$mapping['target']]['real_target']});
    }
    elseif (isset($target_item->{$mapping['target']})) {
      unset($target_item->{$mapping['target']});
    }
  }
  if ($convert_to_array) {
    $target_item = (array) $target_item;
  }

  // Set custom fields
  // http://groups.drupal.org/node/8796
  // http://www.stonemind.net/blog/index.php?s=cck
  // http://civicactions.com/blog/cck_import_and_update
  // http://www.lullabot.com/articles/quick-and-dirty-cck-imports
  $source_item = $batch
    ->currentItem();
  foreach ($source_item['rdf'] as $key => $value) {
    if (empty($key)) {
      continue;
    }
    $value = $source_item['rdf'][$key];
    $fname = drupal_substr($key, 0, 6);
    if ($fname == "field_") {

      // Build up a field value.
      $target_item->{$key} = $value;
    }
    else {

      // Set properties on the node.  There's a couple we know we don't want,
      // because their meaning is site-specific anyway.
      // @todo Replace this logic with the mapping engine for more flexibility.
      if (!in_array($key, array(
        'nid',
        'vid',
        'revision_uid',
        'log',
        'created',
        'changed',
        'revision_timestamp',
        'last_comment_timestamp',
      ))) {
        $target_item->{$key} = $value;
      }
    }
  }

  // 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 (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 (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);
    }
  }

  // Allow other modules to add additional mapping
  // Invokes hook_feeds_atom_rdf_map_alter().
  drupal_alter('feeds_atom_rdf_map', $target_item, $source_item, $source);
  return $target_item;
}