EntityFinder.php in Feeds 8.3
File
src/EntityFinder.php
View source
<?php
namespace Drupal\feeds;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class EntityFinder implements EntityFinderInterface {
protected $entityTypeManager;
protected $entityRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
public function findEntities(string $entity_type_id, string $field, $search, array $bundles = [], $multiple = FALSE) {
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 [];
}
protected function getBundleKey(string $entity_type_id) {
return $this->entityTypeManager
->getDefinition($entity_type_id)
->getKey('bundle');
}
}
Classes
Name |
Description |
EntityFinder |
Searches for existing entities by a certain field. |