--- a/plugins/FeedsProcessor.inc 2012-10-12 07:16:10.000000000 +0530 +++ b/plugins/FeedsProcessor.inc 2015-06-02 15:34:55.604801437 +0530 @@ -578,30 +578,43 @@ * @return * The serial id of an entity if found, 0 otherwise. */ - protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) { + protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) { $query = db_select('feeds_item') ->fields('feeds_item', array('entity_id')) ->condition('feed_nid', $source->feed_nid) ->condition('entity_type', $this->entityType()) ->condition('id', $source->id); - // Iterate through all unique targets and test whether they do already - // exist in the database. + // Iterate through all unique targets and test whether they do already + // exist in the database. + $targets = &drupal_static('FeedsProcessor::existingEntityId', array()); + if (!isset($targets[$this->id])) { + $targets[$this->id] = $this->getMappingTargets(); + } + + $entity_id = 0; + + foreach ($this->uniqueTargets($source, $result) as $target => $value) { - switch ($target) { - case 'url': - $entity_id = $query->condition('url', $value)->execute()->fetchField(); - break; - case 'guid': - $entity_id = $query->condition('guid', $value)->execute()->fetchField(); - break; - } - if (isset($entity_id)) { - // Return with the content id found. - return $entity_id; + if ($target === 'guid' || $target === 'url') { + $entity_id = $query->condition($target, $value)->execute()->fetchField(); + } + if (!$entity_id && !empty($targets[$this->id][$target]['unique_callbacks'])) { + if (!is_array($value)) { + $value = array($value); + } + foreach ($targets[$this->id][$target]['unique_callbacks'] as $callback) { + if (is_callable($callback) && $entity_id = call_user_func_array($callback, array($source, $this->entityType(), $this->config['content_type'], $target, $value))) { + // Stop at the first unique ID returned by a callback. + break; + } + } } + if($entity_id) { + return $entity_id; + } } - return 0; + return $entity_id; }