You are here

public function EntityRepository::loadEntityByConfigTarget in Drupal 8

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

Loads an entity by the config target identifier.

Parameters

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

string $target: The configuration target to load, as returned from \Drupal\Core\Entity\EntityInterface::getConfigTarget().

Return value

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

Throws

\Drupal\Core\Entity\EntityStorageException Thrown if the target identifier is a UUID but the entity type does not support UUIDs.

Overrides EntityRepositoryInterface::loadEntityByConfigTarget

See also

\Drupal\Core\Entity\EntityInterface::getConfigTarget()

File

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

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

public function loadEntityByConfigTarget($entity_type_id, $target) {
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);

  // For configuration entities, the config target is given by the entity ID.
  // @todo Consider adding a method to allow entity types to indicate the
  //   target identifier key rather than hard-coding this check. Issue:
  //   https://www.drupal.org/node/2412983.
  if ($entity_type instanceof ConfigEntityTypeInterface) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($target);
  }
  else {
    $entity = $this
      ->loadEntityByUuid($entity_type_id, $target);
  }
  return $entity;
}