EntityDefinitionUpdateManagerInterface.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, \Drupal\Core\Entity\EntityManagerInterface has methods to
  14. * retrieve the last installed definitions as well as the definitions specified
  15. * by the current codebase. It also has create/update/delete methods to bring
  16. * the former up to date with the latter.
  17. *
  18. * However, it is not the responsibility of the entity manager to decide how to
  19. * report the differences or when to apply each update. This interface is for
  20. * managing that.
  21. *
  22. * This interface also provides methods to retrieve instances of the definitions
  23. * to be updated ready to be manipulated. In fact when definitions change in
  24. * code the system needs to be notified about that and the definitions stored in
  25. * state need to be reconciled with the ones living in code. This typically
  26. * happens in Update API functions, which need to take the system from a known
  27. * state to another known state. Relying on the definitions living in code might
  28. * prevent this, as the system might transition directly to the last available
  29. * state, and thus skipping the intermediate steps. Manipulating the definitions
  30. * in state allows to avoid this and ensures that the various steps of the
  31. * update process are predictable and repeatable.
  32. *
  33. * @see \Drupal\Core\Entity\EntityManagerInterface::getDefinition()
  34. * @see \Drupal\Core\Entity\EntityManagerInterface::getLastInstalledDefinition()
  35. * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldStorageDefinitions()
  36. * @see \Drupal\Core\Entity\EntityManagerInterface::getLastInstalledFieldStorageDefinitions()
  37. * @see hook_update_N()
  38. */
  39. interface EntityDefinitionUpdateManagerInterface {
  40. /**
  41. * Indicates that a definition has just been created.
  42. *
  43. * @var int
  44. */
  45. const DEFINITION_CREATED = 1;
  46. /**
  47. * Indicates that a definition has changes.
  48. *
  49. * @var int
  50. */
  51. const DEFINITION_UPDATED = 2;
  52. /**
  53. * Indicates that a definition has just been deleted.
  54. *
  55. * @var int
  56. */
  57. const DEFINITION_DELETED = 3;
  58. /**
  59. * Checks if there are any definition updates that need to be applied.
  60. *
  61. * @return bool
  62. * TRUE if updates are needed.
  63. */
  64. public function needsUpdates();
  65. /**
  66. * Gets a human readable summary of the detected changes.
  67. *
  68. * @return array
  69. * An associative array keyed by entity type id. Each entry is an array of
  70. * human-readable strings, each describing a change.
  71. */
  72. public function getChangeSummary();
  73. /**
  74. * Applies all the detected valid changes.
  75. *
  76. * Use this with care, as it will apply updates for any module, which will
  77. * lead to unpredictable results.
  78. *
  79. * @throws \Drupal\Core\Entity\EntityStorageException
  80. * This exception is thrown if a change cannot be applied without
  81. * unacceptable data loss. In such a case, the site administrator needs to
  82. * apply some other process, such as a custom update function or a
  83. * migration via the Migrate module.
  84. */
  85. public function applyUpdates();
  86. /**
  87. * Returns an entity type definition ready to be manipulated.
  88. *
  89. * When needing to apply updates to existing entity type definitions, this
  90. * method should always be used to retrieve a definition ready to be
  91. * manipulated.
  92. *
  93. * @param string $entity_type_id
  94. * The entity type identifier.
  95. *
  96. * @return \Drupal\Core\Entity\EntityTypeInterface
  97. * The entity type definition.
  98. */
  99. public function getEntityType($entity_type_id);
  100. /**
  101. * Returns all the entity type definitions, ready to be manipulated.
  102. *
  103. * When needing to apply updates to existing entity type definitions, this
  104. * method should always be used to retrieve all the definitions ready to be
  105. * manipulated.
  106. *
  107. * @return \Drupal\Core\Entity\EntityTypeInterface[]
  108. * The last installed entity type definitions, keyed by the entity type ID.
  109. */
  110. public function getEntityTypes();
  111. /**
  112. * Installs a new entity type definition.
  113. *
  114. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  115. * The entity type definition.
  116. */
  117. public function installEntityType(EntityTypeInterface $entity_type);
  118. /**
  119. * Applies any change performed to the passed entity type definition.
  120. *
  121. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  122. * The entity type definition.
  123. */
  124. public function updateEntityType(EntityTypeInterface $entity_type);
  125. /**
  126. * Uninstalls an entity type definition.
  127. *
  128. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  129. * The entity type definition.
  130. */
  131. public function uninstallEntityType(EntityTypeInterface $entity_type);
  132. /**
  133. * Returns a field storage definition ready to be manipulated.
  134. *
  135. * When needing to apply updates to existing field storage definitions, this
  136. * method should always be used to retrieve a storage definition ready to be
  137. * manipulated.
  138. *
  139. * @param string $name
  140. * The field name.
  141. * @param string $entity_type_id
  142. * The entity type identifier.
  143. *
  144. * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
  145. * The field storage definition.
  146. *
  147. * @todo Make this return a mutable storage definition interface when we have
  148. * one. See https://www.drupal.org/node/2346329.
  149. */
  150. public function getFieldStorageDefinition($name, $entity_type_id);
  151. /**
  152. * Installs a new field storage definition.
  153. *
  154. * @param string $name
  155. * The field storage definition name.
  156. * @param string $entity_type_id
  157. * The target entity type identifier.
  158. * @param string $provider
  159. * The name of the definition provider.
  160. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  161. * The field storage definition.
  162. */
  163. public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition);
  164. /**
  165. * Applies any change performed to the passed field storage definition.
  166. *
  167. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  168. * The field storage definition.
  169. */
  170. public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  171. /**
  172. * Uninstalls a field storage definition.
  173. *
  174. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  175. * The field storage definition.
  176. */
  177. public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  178. }