You are here

public function FieldTargetBase::getUniqueValue in Feeds 8.3

Looks for an existing entity and returns an entity ID if found.

Parameters

\Drupal\feeds\FeedInterface $feed: The feed that is being processed.

string $target: The ID of the field target plugin.

string $key: The property of the field to search on.

string $value: The value to look for.

Return value

string|int|null An entity ID, if found. Null otherwise.

File

src/Plugin/Type/Target/FieldTargetBase.php, line 213

Class

FieldTargetBase
Helper class for field mappers.

Namespace

Drupal\feeds\Plugin\Type\Target

Code

public function getUniqueValue(FeedInterface $feed, $target, $key, $value) {
  $base_fields = \Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions($this->feedType
    ->getProcessor()
    ->entityType());
  if (isset($base_fields[$target])) {
    $field = $target;
  }
  else {
    $field = "{$target}.{$key}";
  }

  // Construct "Unique" query.
  $query = $this
    ->getUniqueQuery()
    ->condition($field, $value);

  // Restrict search to the same bundle if the entity type we import for
  // supports bundles.
  $bundle_key = $this->feedType
    ->getProcessor()
    ->bundleKey();
  if ($bundle_key) {
    $query
      ->condition($bundle_key, $this->feedType
      ->getProcessor()
      ->bundle());
  }

  // Execute "Unique" query.
  if ($result = $query
    ->execute()) {
    return reset($result);
  }
}