EntityDefinitionUpdateManagerInterface.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. /**
  5. * Defines an interface for managing entity definition updates.
  6. *
  7. * During the application lifetime, the definitions of various entity types and
  8. * their data components (e.g., fields for fieldable entity types) can change.
  9. * For example, updated code can be deployed. Some entity handlers may need to
  10. * perform complex or long-running logic in response to the change. For
  11. * example, a SQL-based storage handler may need to update the database schema.
  12. *
  13. * To support this,
  14. * \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface has methods
  15. * to retrieve the last installed definitions as well as the definitions
  16. * specified by the current codebase. It also has create/update/delete methods
  17. * to bring the former up to date with the latter.
  18. *
  19. * However, it is not the responsibility of the entity manager to decide how to
  20. * report the differences or when to apply each update. This interface is for
  21. * managing that.
  22. *
  23. * This interface also provides methods to retrieve instances of the definitions
  24. * to be updated ready to be manipulated. In fact when definitions change in
  25. * code the system needs to be notified about that and the definitions stored in
  26. * state need to be reconciled with the ones living in code. This typically
  27. * happens in Update API functions, which need to take the system from a known
  28. * state to another known state. Relying on the definitions living in code might
  29. * prevent this, as the system might transition directly to the last available
  30. * state, and thus skipping the intermediate steps. Manipulating the definitions
  31. * in state allows to avoid this and ensures that the various steps of the
  32. * update process are predictable and repeatable.
  33. *
  34. * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getDefinition()
  35. * @see \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinition()
  36. * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldStorageDefinitions()
  37. * @see \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledFieldStorageDefinitions()
  38. * @see hook_update_N()
  39. */
  40. interface EntityDefinitionUpdateManagerInterface {
  41. /**
  42. * Indicates that a definition has just been created.
  43. *
  44. * @var int
  45. */
  46. const DEFINITION_CREATED = 1;
  47. /**
  48. * Indicates that a definition has changes.
  49. *
  50. * @var int
  51. */
  52. const DEFINITION_UPDATED = 2;
  53. /**
  54. * Indicates that a definition has just been deleted.
  55. *
  56. * @var int
  57. */
  58. const DEFINITION_DELETED = 3;
  59. /**
  60. * Checks if there are any definition updates that need to be applied.
  61. *
  62. * @return bool
  63. * TRUE if updates are needed.
  64. */
  65. public function needsUpdates();
  66. /**
  67. * Gets a human readable summary of the detected changes.
  68. *
  69. * @return array
  70. * An associative array keyed by entity type id. Each entry is an array of
  71. * human-readable strings, each describing a change.
  72. */
  73. public function getChangeSummary();
  74. /**
  75. * Gets a list of changes to entity type and field storage definitions.
  76. *
  77. * @return array
  78. * An associative array keyed by entity type ID of change descriptors. Every
  79. * entry is an associative array with the following optional keys:
  80. * - entity_type: a scalar having one value among:
  81. * - EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED
  82. * - EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED
  83. * - field_storage_definitions: an associative array keyed by field name of
  84. * scalars having one value among:
  85. * - EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED
  86. * - EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED
  87. * - EntityDefinitionUpdateManagerInterface::DEFINITION_DELETED
  88. */
  89. public function getChangeList();
  90. /**
  91. * Applies all the detected valid changes.
  92. *
  93. * Use this with care, as it will apply updates for any module, which will
  94. * lead to unpredictable results.
  95. *
  96. * @throws \Drupal\Core\Entity\EntityStorageException
  97. * This exception is thrown if a change cannot be applied without
  98. * unacceptable data loss. In such a case, the site administrator needs to
  99. * apply some other process, such as a custom update function or a
  100. * migration via the Migrate module.
  101. *
  102. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  103. * \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::getChangeList()
  104. * and execute each entity type and field storage update manually instead.
  105. *
  106. * @see https://www.drupal.org/node/3034742
  107. */
  108. public function applyUpdates();
  109. /**
  110. * Returns an entity type definition ready to be manipulated.
  111. *
  112. * When needing to apply updates to existing entity type definitions, this
  113. * method should always be used to retrieve a definition ready to be
  114. * manipulated.
  115. *
  116. * @param string $entity_type_id
  117. * The entity type identifier.
  118. *
  119. * @return \Drupal\Core\Entity\EntityTypeInterface
  120. * The entity type definition.
  121. */
  122. public function getEntityType($entity_type_id);
  123. /**
  124. * Returns all the entity type definitions, ready to be manipulated.
  125. *
  126. * When needing to apply updates to existing entity type definitions, this
  127. * method should always be used to retrieve all the definitions ready to be
  128. * manipulated.
  129. *
  130. * @return \Drupal\Core\Entity\EntityTypeInterface[]
  131. * The last installed entity type definitions, keyed by the entity type ID.
  132. */
  133. public function getEntityTypes();
  134. /**
  135. * Installs a new entity type definition.
  136. *
  137. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  138. * The entity type definition.
  139. */
  140. public function installEntityType(EntityTypeInterface $entity_type);
  141. /**
  142. * Installs a new fieldable entity type definition.
  143. *
  144. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  145. * The entity type definition.
  146. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions
  147. * The entity type's field storage definitions.
  148. */
  149. public function installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions);
  150. /**
  151. * Applies any change performed to the passed entity type definition.
  152. *
  153. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  154. * The entity type definition.
  155. */
  156. public function updateEntityType(EntityTypeInterface $entity_type);
  157. /**
  158. * Uninstalls an entity type definition.
  159. *
  160. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  161. * The entity type definition.
  162. */
  163. public function uninstallEntityType(EntityTypeInterface $entity_type);
  164. /**
  165. * Applies any change performed to a fieldable entity type definition.
  166. *
  167. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  168. * The updated entity type definition.
  169. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions
  170. * The updated field storage definitions, including possibly new ones.
  171. * @param array &$sandbox
  172. * (optional) A sandbox array provided by a hook_update_N() implementation
  173. * or a Batch API callback. If the entity schema update requires a data
  174. * migration, this parameter is mandatory. Defaults to NULL.
  175. */
  176. public function updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array &$sandbox = NULL);
  177. /**
  178. * Returns a field storage definition ready to be manipulated.
  179. *
  180. * When needing to apply updates to existing field storage definitions, this
  181. * method should always be used to retrieve a storage definition ready to be
  182. * manipulated.
  183. *
  184. * @param string $name
  185. * The field name.
  186. * @param string $entity_type_id
  187. * The entity type identifier.
  188. *
  189. * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
  190. * The field storage definition.
  191. *
  192. * @todo Make this return a mutable storage definition interface when we have
  193. * one. See https://www.drupal.org/node/2346329.
  194. */
  195. public function getFieldStorageDefinition($name, $entity_type_id);
  196. /**
  197. * Installs a new field storage definition.
  198. *
  199. * @param string $name
  200. * The field storage definition name.
  201. * @param string $entity_type_id
  202. * The target entity type identifier.
  203. * @param string $provider
  204. * The name of the definition provider.
  205. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  206. * The field storage definition.
  207. */
  208. public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition);
  209. /**
  210. * Applies any change performed to the passed field storage definition.
  211. *
  212. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  213. * The field storage definition.
  214. */
  215. public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  216. /**
  217. * Uninstalls a field storage definition.
  218. *
  219. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  220. * The field storage definition.
  221. */
  222. public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  223. }