You are here

public function EntityRepository::loadEntityByUuid in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::loadEntityByUuid()

Loads an entity by UUID.

Note that some entity types may not support UUIDs.

Parameters

string $entity_type_id: The entity type ID to load from.

string $uuid: The UUID of the entity to load.

Return value

\Drupal\Core\Entity\EntityInterface|null The entity object, or NULL if there is no entity with the given UUID.

Throws

\Drupal\Core\Entity\EntityStorageException Thrown in case the requested entity type does not support UUIDs.

Overrides EntityRepositoryInterface::loadEntityByUuid

1 call to EntityRepository::loadEntityByUuid()
EntityRepository::loadEntityByConfigTarget in core/lib/Drupal/Core/Entity/EntityRepository.php
Loads an entity by the config target identifier.

File

core/lib/Drupal/Core/Entity/EntityRepository.php, line 50
Contains \Drupal\Core\Entity\EntityRepository.

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 reset($entities);
}