EntityDefinitionUpdateManagerInterface.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Installs a new entity type definition.
  102. *
  103. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  104. * The entity type definition.
  105. */
  106. public function installEntityType(EntityTypeInterface $entity_type);
  107. /**
  108. * Applies any change performed to the passed entity type definition.
  109. *
  110. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  111. * The entity type definition.
  112. */
  113. public function updateEntityType(EntityTypeInterface $entity_type);
  114. /**
  115. * Uninstalls an entity type definition.
  116. *
  117. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  118. * The entity type definition.
  119. */
  120. public function uninstallEntityType(EntityTypeInterface $entity_type);
  121. /**
  122. * Returns a field storage definition ready to be manipulated.
  123. *
  124. * When needing to apply updates to existing field storage definitions, this
  125. * method should always be used to retrieve a storage definition ready to be
  126. * manipulated.
  127. *
  128. * @param string $name
  129. * The field name.
  130. * @param string $entity_type_id
  131. * The entity type identifier.
  132. *
  133. * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
  134. * The field storage definition.
  135. *
  136. * @todo Make this return a mutable storage definition interface when we have
  137. * one. See https://www.drupal.org/node/2346329.
  138. */
  139. public function getFieldStorageDefinition($name, $entity_type_id);
  140. /**
  141. * Installs a new field storage definition.
  142. *
  143. * @param string $name
  144. * The field storage definition name.
  145. * @param string $entity_type_id
  146. * The target entity type identifier.
  147. * @param string $provider
  148. * The name of the definition provider.
  149. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  150. * The field storage definition.
  151. */
  152. public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition);
  153. /**
  154. * Applies any change performed to the passed field storage definition.
  155. *
  156. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  157. * The field storage definition.
  158. */
  159. public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  160. /**
  161. * Uninstalls a field storage definition.
  162. *
  163. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  164. * The field storage definition.
  165. */
  166. public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
  167. }