Drupal investigation

field.purge.inc 7.2KB

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