field.purge.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Provides support for field data purge after mass deletion.
  5. */
  6. use Drupal\Core\Field\FieldDefinitionInterface;
  7. use Drupal\Core\Field\FieldException;
  8. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  9. /**
  10. * @defgroup field_purge Field API bulk data deletion
  11. * @{
  12. * Cleans up after Field API bulk deletion operations.
  13. *
  14. * Field API provides functions for deleting data attached to individual
  15. * entities as well as deleting entire fields or field storages in a single
  16. * operation.
  17. *
  18. * When a single entity is deleted, the Entity storage performs the
  19. * following operations:
  20. * - Invoking the method \Drupal\Core\Field\FieldItemListInterface::delete() for
  21. * each field on the entity. A file field type might use this method to delete
  22. * uploaded files from the filesystem.
  23. * - Removing the data from storage.
  24. * - Invoking the global hook_entity_delete() for all modules that implement it.
  25. * Each hook implementation receives the entity being deleted and can operate
  26. * on whichever subset of the entity's bundle's fields it chooses to.
  27. *
  28. * Similar operations are performed on deletion of a single entity revision.
  29. *
  30. * When a bundle, field or field storage is deleted, it is not practical to
  31. * perform those operations immediately on every affected entity in a single
  32. * page request; there could be thousands or millions of them. Instead, the
  33. * appropriate field data items, fields, and/or field storages are marked as
  34. * deleted so that subsequent load or query operations will not return them.
  35. * Later, a separate process cleans up, or "purges", the marked-as-deleted data
  36. * by going through the three-step process described above and, finally,
  37. * removing deleted field storage and field records.
  38. *
  39. * Purging field data is made somewhat tricky by the fact that, while
  40. * $entity->delete() has a complete entity to pass to the various deletion
  41. * steps, the Field API purge process only has the field data it has previously
  42. * stored. It cannot reconstruct complete original entities to pass to the
  43. * deletion operations. It is even possible that the original entity to which
  44. * some Field API data was attached has been itself deleted before the field
  45. * purge operation takes place.
  46. *
  47. * Field API resolves this problem by using stub entities during purge
  48. * operations, containing only the information from the original entity that
  49. * Field API knows about: entity type, ID, revision ID, and bundle. It also
  50. * contains the field data for whichever field is currently being purged.
  51. *
  52. * See @link field Field API @endlink for information about the other parts of
  53. * the Field API.
  54. */
  55. /**
  56. * Purges a batch of deleted Field API data, field storages, or fields.
  57. *
  58. * This function will purge deleted field data in batches. The batch size
  59. * is defined as an argument to the function, and once each batch is finished,
  60. * it continues with the next batch until all have completed. If a deleted field
  61. * with no remaining data records is found, the field itself will
  62. * be purged. If a deleted field storage with no remaining fields is found, the
  63. * field storage itself will be purged.
  64. *
  65. * @param int $batch_size
  66. * The maximum number of field data records to purge before returning.
  67. * @param string $field_storage_unique_id
  68. * (optional) Limit the purge to a specific field storage. Defaults to NULL.
  69. */
  70. function field_purge_batch($batch_size, $field_storage_unique_id = NULL) {
  71. /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
  72. $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
  73. $fields = $deleted_fields_repository->getFieldDefinitions($field_storage_unique_id);
  74. $info = \Drupal::entityManager()->getDefinitions();
  75. foreach ($fields as $field) {
  76. $entity_type = $field->getTargetEntityTypeId();
  77. // We cannot purge anything if the entity type is unknown (e.g. the
  78. // providing module was uninstalled).
  79. // @todo Revisit after https://www.drupal.org/node/2080823.
  80. if (!isset($info[$entity_type])) {
  81. \Drupal::logger('field')->warning("Cannot remove field @field_name because the entity type is unknown: %entity_type", ['@field_name' => $field->getName(), '%entity_type' => $entity_type]);
  82. continue;
  83. }
  84. $count_purged = \Drupal::entityManager()->getStorage($entity_type)->purgeFieldData($field, $batch_size);
  85. if ($count_purged < $batch_size || $count_purged == 0) {
  86. // No field data remains for the field, so we can remove it.
  87. field_purge_field($field);
  88. }
  89. $batch_size -= $count_purged;
  90. // Only delete up to the maximum number of records.
  91. if ($batch_size == 0) {
  92. break;
  93. }
  94. }
  95. // Retrieve all deleted field storages. Any that have no fields can be purged.
  96. foreach ($deleted_fields_repository->getFieldStorageDefinitions() as $field_storage) {
  97. if ($field_storage_unique_id && $field_storage->getUniqueStorageIdentifier() != $field_storage_unique_id) {
  98. // If a specific UUID is provided, only purge the corresponding field.
  99. continue;
  100. }
  101. // We cannot purge anything if the entity type is unknown (e.g. the
  102. // providing module was uninstalled).
  103. // @todo Revisit after https://www.drupal.org/node/2080823.
  104. if (!isset($info[$field_storage->getTargetEntityTypeId()])) {
  105. continue;
  106. }
  107. $fields = $deleted_fields_repository->getFieldDefinitions($field_storage->getUniqueStorageIdentifier());
  108. if (empty($fields)) {
  109. field_purge_field_storage($field_storage);
  110. }
  111. }
  112. }
  113. /**
  114. * Purges a field record from the database.
  115. *
  116. * This function assumes all data for the field has already been purged and
  117. * should only be called by field_purge_batch().
  118. *
  119. * @param \Drupal\Core\Field\FieldDefinitionInterface $field
  120. * The field to purge.
  121. */
  122. function field_purge_field(FieldDefinitionInterface $field) {
  123. /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
  124. $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
  125. $deleted_fields_repository->removeFieldDefinition($field);
  126. // Invoke external hooks after the cache is cleared for API consistency.
  127. \Drupal::moduleHandler()->invokeAll('field_purge_field', [$field]);
  128. }
  129. /**
  130. * Purges a field record from the database.
  131. *
  132. * This function assumes all fields for the field storage has already been
  133. * purged, and should only be called by field_purge_batch().
  134. *
  135. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage
  136. * The field storage to purge.
  137. *
  138. * @throws \Drupal\Core\Field\FieldException
  139. */
  140. function field_purge_field_storage(FieldStorageDefinitionInterface $field_storage) {
  141. /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
  142. $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
  143. $fields = $deleted_fields_repository->getFieldDefinitions($field_storage->getUniqueStorageIdentifier());
  144. if (count($fields) > 0) {
  145. throw new FieldException(t('Attempt to purge a field storage @field_name that still has fields.', ['@field_name' => $field_storage->getName()]));
  146. }
  147. $deleted_fields_repository->removeFieldStorageDefinition($field_storage);
  148. // Notify the storage layer.
  149. \Drupal::entityManager()->getStorage($field_storage->getTargetEntityTypeId())->finalizePurge($field_storage);
  150. // Invoke external hooks after the cache is cleared for API consistency.
  151. \Drupal::moduleHandler()->invokeAll('field_purge_field_storage', [$field_storage]);
  152. }
  153. /**
  154. * @} End of "defgroup field_purge".
  155. */