EntityRepositoryInterface.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. /**
  4. * Provides an interface for an entity repository.
  5. */
  6. interface EntityRepositoryInterface {
  7. const CONTEXT_ID_LEGACY_CONTEXT_OPERATION = '@entity.repository:legacy_context_operation';
  8. /**
  9. * Loads an entity by UUID.
  10. *
  11. * Note that some entity types may not support UUIDs.
  12. *
  13. * @param string $entity_type_id
  14. * The entity type ID to load from.
  15. * @param string $uuid
  16. * The UUID of the entity to load.
  17. *
  18. * @return \Drupal\Core\Entity\EntityInterface|null
  19. * The entity object, or NULL if there is no entity with the given UUID.
  20. *
  21. * @throws \Drupal\Core\Entity\EntityStorageException
  22. * Thrown in case the requested entity type does not support UUIDs.
  23. */
  24. public function loadEntityByUuid($entity_type_id, $uuid);
  25. /**
  26. * Loads an entity by the config target identifier.
  27. *
  28. * @param string $entity_type_id
  29. * The entity type ID to load from.
  30. * @param string $target
  31. * The configuration target to load, as returned from
  32. * \Drupal\Core\Entity\EntityInterface::getConfigTarget().
  33. *
  34. * @return \Drupal\Core\Entity\EntityInterface|null
  35. * The entity object, or NULL if there is no entity with the given config
  36. * target identifier.
  37. *
  38. * @throws \Drupal\Core\Entity\EntityStorageException
  39. * Thrown if the target identifier is a UUID but the entity type does not
  40. * support UUIDs.
  41. *
  42. * @see \Drupal\Core\Entity\EntityInterface::getConfigTarget()
  43. */
  44. public function loadEntityByConfigTarget($entity_type_id, $target);
  45. /**
  46. * Gets the entity translation to be used in the given context.
  47. *
  48. * This will check whether a translation for the desired language is available
  49. * and if not, it will fall back to the most appropriate translation based on
  50. * the provided context.
  51. *
  52. * @param \Drupal\Core\Entity\EntityInterface $entity
  53. * The entity whose translation will be returned.
  54. * @param string $langcode
  55. * (optional) The language of the current context. Defaults to the current
  56. * content language.
  57. * @param array $context
  58. * (optional) An associative array of arbitrary data that can be useful to
  59. * determine the proper fallback sequence.
  60. *
  61. * @return \Drupal\Core\Entity\EntityInterface
  62. * An entity object for the translated data.
  63. *
  64. * @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
  65. */
  66. public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = []);
  67. /**
  68. * Retrieves the active entity variant matching the specified context.
  69. *
  70. * If an entity type is revisionable and/or translatable, which entity variant
  71. * should be handled depends on the context in which the manipulation happens.
  72. * Based on the specified contextual information, revision and translation
  73. * negotiation needs to be performed to return the active variant, that is the
  74. * most up-to-date entity variant in the context scope. This may or may not be
  75. * an entity variant intended for unprivileged user consumption, in fact it
  76. * might be a work in progress containing yet to be published information. The
  77. * active variant should always be retrieved when editing an entity, both in
  78. * form and in REST workflows, or previewing the related changes.
  79. *
  80. * The negotiation process will not perform any access check, so it is the
  81. * responsibility of the caller to verify that the user manipulating the
  82. * entity variant is actually allowed to do so.
  83. *
  84. * @param string $entity_type_id
  85. * The entity type identifier.
  86. * @param int|string $entity_id
  87. * An entity identifier.
  88. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  89. * (optional) An associative array of objects representing the contexts the
  90. * entity will be edited in keyed by fully qualified context ID. Defaults to
  91. * the currently available contexts.
  92. *
  93. * @return \Drupal\Core\Entity\EntityInterface|null
  94. * An entity object variant or NULL if the entity does not exist.
  95. */
  96. public function getActive($entity_type_id, $entity_id, array $contexts = NULL);
  97. /**
  98. * Retrieves the active entity variants matching the specified context.
  99. *
  100. * @param string $entity_type_id
  101. * The entity type identifier.
  102. * @param int[]|string[] $entity_ids
  103. * An array of entity identifiers.
  104. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  105. * (optional) An associative array of objects representing the contexts the
  106. * entity will be edited in keyed by fully qualified context ID. Defaults to
  107. * the currently available contexts.
  108. *
  109. * @return \Drupal\Core\Entity\EntityInterface
  110. * An array of entity object variants keyed by entity ID.
  111. *
  112. * @see getActive()
  113. */
  114. public function getActiveMultiple($entity_type_id, array $entity_ids, array $contexts = NULL);
  115. /**
  116. * Retrieves the canonical entity variant matching the specified context.
  117. *
  118. * If an entity type is revisionable and/or translatable, which entity variant
  119. * should be handled depends on the context in which the manipulation happens.
  120. * This will return the fittest entity variant intended for unprivileged user
  121. * consumption matching the specified context. This is typically the variant
  122. * that would be displayed on the entity's canonical route.
  123. *
  124. * The negotiation process will not perform any access check, so it is the
  125. * responsibility of the caller to verify that the user manipulating the
  126. * entity variant is actually allowed to do so.
  127. *
  128. * @param string $entity_type_id
  129. * The entity type identifier.
  130. * @param int|string $entity_id
  131. * An entity identifier.
  132. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  133. * (optional) An associative array of objects representing the contexts the
  134. * entity will be edited in keyed by fully qualified context ID. Defaults to
  135. * the currently available contexts.
  136. *
  137. * @return \Drupal\Core\Entity\EntityInterface|null
  138. * An entity object variant or NULL if the entity does not exist.
  139. */
  140. public function getCanonical($entity_type_id, $entity_id, array $contexts = NULL);
  141. /**
  142. * Retrieves the canonical entity variants matching the specified context.
  143. *
  144. * @param string $entity_type_id
  145. * The entity type identifier.
  146. * @param int[]|string[] $entity_ids
  147. * An array of entity identifiers.
  148. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  149. * (optional) An associative array of objects representing the contexts the
  150. * entity will be edited in keyed by fully qualified context ID. Defaults to
  151. * the currently available contexts.
  152. *
  153. * @return \Drupal\Core\Entity\EntityInterface
  154. * An array of entity object variants keyed by entity ID.
  155. *
  156. * @see getCanonical()
  157. */
  158. public function getCanonicalMultiple($entity_type_id, array $entity_ids, array $contexts = NULL);
  159. }