annotate core/modules/field/field.purge.inc @ 0:4c8ae668cc8c

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