class EntityFinder in Feeds 8.3
Searches for existing entities by a certain field.
Hierarchy
- class \Drupal\feeds\EntityFinder implements EntityFinderInterface
Expanded class hierarchy of EntityFinder
1 file declares its use of EntityFinder
- EntityFinderTest.php in tests/
src/ Unit/ EntityFinderTest.php
1 string reference to 'EntityFinder'
1 service uses EntityFinder
File
- src/
EntityFinder.php, line 11
Namespace
Drupal\feedsView source
class EntityFinder implements EntityFinderInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a new EntityFinder object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
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 [];
}
/**
* Returns the entity type's bundle key.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return string
* The bundle key of the entity type.
*/
protected function getBundleKey(string $entity_type_id) {
return $this->entityTypeManager
->getDefinition($entity_type_id)
->getKey('bundle');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityFinder:: |
protected | property | The entity repository service. | |
EntityFinder:: |
protected | property | The entity type manager. | |
EntityFinder:: |
public | function |
Searches for entities by entity key. Overrides EntityFinderInterface:: |
|
EntityFinder:: |
protected | function | Returns the entity type's bundle key. | |
EntityFinder:: |
public | function | Constructs a new EntityFinder object. |