You are here

public function EntityFinder::findEntities in Feeds 8.3

Searches for entities by entity key.

Parameters

string $entity_type_id: The entity type ID.

string $field: The subfield to search in.

string|int $search: The value to search for.

array $bundles: (optional) The bundles to restrict the search by.

bool $multiple: (optional) Whether or not to select multiple results. Defaults to FALSE.

Return value

int[] A list of entity ID's that were found.

Overrides EntityFinderInterface::findEntities

File

src/EntityFinder.php, line 43

Class

EntityFinder
Searches for existing entities by a certain field.

Namespace

Drupal\feeds

Code

public function findEntities(string $entity_type_id, string $field, $search, array $bundles = [], $multiple = FALSE) {

  // When referencing by UUID, use the EntityRepository service.
  if ($field === 'uuid') {
    if (NULL !== ($entity = $this->entityRepository
      ->loadEntityByUuid($entity_type_id, $search))) {
      return [
        $entity
          ->id(),
      ];
    }
  }
  else {
    $query = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getQuery();
    if (!empty($bundles)) {
      $query
        ->condition($this
        ->getBundleKey($entity_type_id), $bundles, 'IN');
    }
    $query
      ->condition($field, $search);
    if (!$multiple) {
      $query
        ->range(0, 1);
    }
    return array_filter($query
      ->execute());
  }
  return [];
}