diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 627bb83..1c3db15 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -408,6 +408,17 @@ public function getConfigDependencyName() { /** * {@inheritdoc} */ + public function getConfigTarget() { + // For configuration entities, use the config ID for the config target + // identifier. This ensures that default configuration (which does not yet + // have UUIDs) can be provided and installed with references to the target, + // and also makes config dependencies more readable. + return $this->id(); + } + + /** + * {@inheritdoc} + */ public function onDependencyRemoval(array $dependencies) { } diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index d9b7b4b..2b2a1c5 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -557,4 +557,13 @@ public function getConfigDependencyName() { return $this->getEntityTypeId() . ':' . $this->bundle() . ':' . $this->uuid(); } + /** + * {@inheritdoc} + */ + public function getConfigTarget() { + // For content entities, use the UUID for the config target identifier. + // This ensures that references to the target can be deployed reliably. + return $this->uuid(); + } + } diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index 674078d..aced311 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -418,4 +418,15 @@ public function getCacheTags(); */ public function getConfigDependencyName(); + /** + * Gets the configuration target identifier for the entity. + * + * Used to supply the correct format for storing a reference targeting this + * entity in other configuration. + * + * @return string + * The configuration target identifier. + */ + public function getConfigTarget(); + } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 7d7eded..b703c40 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -966,6 +966,27 @@ public function loadEntityByUuid($entity_type_id, $uuid) { /** * {@inheritdoc} */ + public function loadEntityByConfigTarget($entity_type_id, $target) { + $entity_type = $this->getDefinition($entity_type_id); + + // For configuration entities, the config target is given by the entity ID. + // @todo Should we allow entity types to return their target identifier key + // rather than hard-coding this check? + if ($entity_type instanceof ConfigEntityType) { + $entity = $this->getStorage($entity_type_id)->load($target); + } + + // For content entities, the config target is given by the UUID. + else { + $entity = $this->loadEntityByUuid($entity_type_id, $target); + } + + return $entity; + } + + /** + * {@inheritdoc} + */ public function getEntityTypeFromClass($class_name) { // Check the already calculated classes first. diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php index 13f9862..7a99cc3 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -460,6 +460,26 @@ public function getFormModeOptions($entity_type_id, $include_disabled = FALSE); public function loadEntityByUuid($entity_type_id, $uuid); /** + * Loads an entity by the config target identifier. + * + * @param string $entity_type_id + * The entity type ID to load from. + * @param string $target + * The configuration target to load, as returned from + * \Drupal\Core\Entity\EntityInterface::getConfigTarget(). + * + * @return \Drupal\Core\Entity\EntityInterface|FALSE + * The entity object, or FALSE if there is no entity with the given UUID. + * + * @throws \Drupal\Core\Entity\EntityStorageException + * Thrown if the target identifier is a UUID but the entity type does not + * support UUIDs. + * + * @see \Drupal\Core\Entity\EntityInterface::getConfigTarget() + */ + public function loadEntityByConfigTarget($entity_type_id, $target); + + /** * Returns the entity type ID based on the class that is called on. * * Compares the class this is called on against the known entity classes