You are here

protected function FeedsNodeProcessor::existingEntityId in Feeds 8.2

Get nid of an existing feed item node if available.

Overrides FeedsProcessor::existingEntityId

File

lib/Drupal/feeds/Plugin/feeds/processor/FeedsNodeProcessor.php, line 359
Contains \Drupal\feeds\Plugin\feeds\fetcher\FeedsNodeProcessor.

Class

FeedsNodeProcessor
Defines a node processor.

Namespace

Drupal\feeds\Plugin\feeds\processor

Code

protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
  if ($nid = parent::existingEntityId($source, $result)) {
    return $nid;
  }

  // Iterate through all unique targets and test whether they do already
  // exist in the database.
  foreach ($this
    ->uniqueTargets($source, $result) as $target => $value) {
    switch ($target) {
      case 'nid':
        $nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(
          ':nid' => $value,
        ))
          ->fetchField();
        break;
      case 'title':
        $nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
          ':title' => $value,
          ':type' => $this
            ->bundle(),
        ))
          ->fetchField();
        break;
      case 'feeds_source':
        if ($id = feeds_get_importer_id($this
          ->bundle())) {
          $nid = db_query("SELECT fs.feed_nid FROM {node} n JOIN {feeds_source} fs ON n.nid = fs.feed_nid WHERE fs.id = :id AND fs.source = :source", array(
            ':id' => $id,
            ':source' => $value,
          ))
            ->fetchField();
        }
        break;
    }
    if ($nid) {

      // Return with the first nid found.
      return $nid;
    }
  }
  return 0;
}