function EntityRepository::loadEntityByUuid

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::loadEntityByUuid()
  2. 10 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::loadEntityByUuid()
  3. 8.9.x core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::loadEntityByUuid()

File

core/lib/Drupal/Core/Entity/EntityRepository.php, line 56

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

public function loadEntityByUuid($entity_type_id, $uuid) {
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  if (!$uuid_key = $entity_type->getKey('uuid')) {
    throw new EntityStorageException("Entity type {$entity_type_id} does not support UUIDs.");
  }
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadByProperties([
    $uuid_key => $uuid,
  ]);
  return $entities ? reset($entities) : NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.