Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/field.purge.inc @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
3 /** | 3 /** |
4 * @file | 4 * @file |
5 * Provides support for field data purge after mass deletion. | 5 * Provides support for field data purge after mass deletion. |
6 */ | 6 */ |
7 | 7 |
8 use Drupal\Core\Field\FieldDefinitionInterface; | |
8 use Drupal\Core\Field\FieldException; | 9 use Drupal\Core\Field\FieldException; |
9 use Drupal\field\Entity\FieldStorageConfig; | 10 use Drupal\Core\Field\FieldStorageDefinitionInterface; |
10 use Drupal\field\FieldStorageConfigInterface; | |
11 use Drupal\field\FieldConfigInterface; | |
12 | 11 |
13 /** | 12 /** |
14 * @defgroup field_purge Field API bulk data deletion | 13 * @defgroup field_purge Field API bulk data deletion |
15 * @{ | 14 * @{ |
16 * Cleans up after Field API bulk deletion operations. | 15 * Cleans up after Field API bulk deletion operations. |
65 * it continues with the next batch until all have completed. If a deleted field | 64 * it continues with the next batch until all have completed. If a deleted field |
66 * with no remaining data records is found, the field itself will | 65 * with no remaining data records is found, the field itself will |
67 * be purged. If a deleted field storage with no remaining fields is found, the | 66 * be purged. If a deleted field storage with no remaining fields is found, the |
68 * field storage itself will be purged. | 67 * field storage itself will be purged. |
69 * | 68 * |
70 * @param $batch_size | 69 * @param int $batch_size |
71 * The maximum number of field data records to purge before returning. | 70 * The maximum number of field data records to purge before returning. |
72 * @param string $field_storage_uuid | 71 * @param string $field_storage_unique_id |
73 * (optional) Limit the purge to a specific field storage. | 72 * (optional) Limit the purge to a specific field storage. Defaults to NULL. |
74 */ | 73 */ |
75 function field_purge_batch($batch_size, $field_storage_uuid = NULL) { | 74 function field_purge_batch($batch_size, $field_storage_unique_id = NULL) { |
76 $properties = [ | 75 /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */ |
77 'deleted' => TRUE, | 76 $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository'); |
78 'include_deleted' => TRUE, | 77 |
79 ]; | 78 $fields = $deleted_fields_repository->getFieldDefinitions($field_storage_unique_id); |
80 if ($field_storage_uuid) { | |
81 $properties['field_storage_uuid'] = $field_storage_uuid; | |
82 } | |
83 $fields = entity_load_multiple_by_properties('field_config', $properties); | |
84 | 79 |
85 $info = \Drupal::entityManager()->getDefinitions(); | 80 $info = \Drupal::entityManager()->getDefinitions(); |
86 foreach ($fields as $field) { | 81 foreach ($fields as $field) { |
87 $entity_type = $field->getTargetEntityTypeId(); | 82 $entity_type = $field->getTargetEntityTypeId(); |
88 | 83 |
89 // We cannot purge anything if the entity type is unknown (e.g. the | 84 // We cannot purge anything if the entity type is unknown (e.g. the |
90 // providing module was uninstalled). | 85 // providing module was uninstalled). |
91 // @todo Revisit after https://www.drupal.org/node/2080823. | 86 // @todo Revisit after https://www.drupal.org/node/2080823. |
92 if (!isset($info[$entity_type])) { | 87 if (!isset($info[$entity_type])) { |
88 \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]); | |
93 continue; | 89 continue; |
94 } | 90 } |
95 | 91 |
96 $count_purged = \Drupal::entityManager()->getStorage($entity_type)->purgeFieldData($field, $batch_size); | 92 $count_purged = \Drupal::entityManager()->getStorage($entity_type)->purgeFieldData($field, $batch_size); |
97 if ($count_purged < $batch_size || $count_purged == 0) { | 93 if ($count_purged < $batch_size || $count_purged == 0) { |
104 break; | 100 break; |
105 } | 101 } |
106 } | 102 } |
107 | 103 |
108 // Retrieve all deleted field storages. Any that have no fields can be purged. | 104 // Retrieve all deleted field storages. Any that have no fields can be purged. |
109 $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: []; | 105 foreach ($deleted_fields_repository->getFieldStorageDefinitions() as $field_storage) { |
110 foreach ($deleted_storages as $field_storage) { | 106 if ($field_storage_unique_id && $field_storage->getUniqueStorageIdentifier() != $field_storage_unique_id) { |
111 $field_storage = new FieldStorageConfig($field_storage); | |
112 if ($field_storage_uuid && $field_storage->uuid() != $field_storage_uuid) { | |
113 // If a specific UUID is provided, only purge the corresponding field. | 107 // If a specific UUID is provided, only purge the corresponding field. |
114 continue; | 108 continue; |
115 } | 109 } |
116 | 110 |
117 // We cannot purge anything if the entity type is unknown (e.g. the | 111 // We cannot purge anything if the entity type is unknown (e.g. the |
119 // @todo Revisit after https://www.drupal.org/node/2080823. | 113 // @todo Revisit after https://www.drupal.org/node/2080823. |
120 if (!isset($info[$field_storage->getTargetEntityTypeId()])) { | 114 if (!isset($info[$field_storage->getTargetEntityTypeId()])) { |
121 continue; | 115 continue; |
122 } | 116 } |
123 | 117 |
124 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); | 118 $fields = $deleted_fields_repository->getFieldDefinitions($field_storage->getUniqueStorageIdentifier()); |
125 if (empty($fields)) { | 119 if (empty($fields)) { |
126 field_purge_field_storage($field_storage); | 120 field_purge_field_storage($field_storage); |
127 } | 121 } |
128 } | 122 } |
129 } | 123 } |
132 * Purges a field record from the database. | 126 * Purges a field record from the database. |
133 * | 127 * |
134 * This function assumes all data for the field has already been purged and | 128 * This function assumes all data for the field has already been purged and |
135 * should only be called by field_purge_batch(). | 129 * should only be called by field_purge_batch(). |
136 * | 130 * |
137 * @param $field | 131 * @param \Drupal\Core\Field\FieldDefinitionInterface $field |
138 * The field record to purge. | 132 * The field to purge. |
139 */ | 133 */ |
140 function field_purge_field(FieldConfigInterface $field) { | 134 function field_purge_field(FieldDefinitionInterface $field) { |
141 $state = \Drupal::state(); | 135 /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */ |
142 $deleted_fields = $state->get('field.field.deleted'); | 136 $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository'); |
143 unset($deleted_fields[$field->uuid()]); | 137 $deleted_fields_repository->removeFieldDefinition($field); |
144 $state->set('field.field.deleted', $deleted_fields); | |
145 | 138 |
146 // Invoke external hooks after the cache is cleared for API consistency. | 139 // Invoke external hooks after the cache is cleared for API consistency. |
147 \Drupal::moduleHandler()->invokeAll('field_purge_field', [$field]); | 140 \Drupal::moduleHandler()->invokeAll('field_purge_field', [$field]); |
148 } | 141 } |
149 | 142 |
151 * Purges a field record from the database. | 144 * Purges a field record from the database. |
152 * | 145 * |
153 * This function assumes all fields for the field storage has already been | 146 * This function assumes all fields for the field storage has already been |
154 * purged, and should only be called by field_purge_batch(). | 147 * purged, and should only be called by field_purge_batch(). |
155 * | 148 * |
156 * @param \Drupal\field\FieldStorageConfigInterface $field_storage | 149 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage |
157 * The field storage to purge. | 150 * The field storage to purge. |
158 * | 151 * |
159 * @throws Drupal\field\FieldException | 152 * @throws \Drupal\Core\Field\FieldException |
160 */ | 153 */ |
161 function field_purge_field_storage(FieldStorageConfigInterface $field_storage) { | 154 function field_purge_field_storage(FieldStorageDefinitionInterface $field_storage) { |
162 $fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); | 155 /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */ |
156 $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository'); | |
157 | |
158 $fields = $deleted_fields_repository->getFieldDefinitions($field_storage->getUniqueStorageIdentifier()); | |
163 if (count($fields) > 0) { | 159 if (count($fields) > 0) { |
164 throw new FieldException(t('Attempt to purge a field storage @field_name that still has fields.', ['@field_name' => $field_storage->getName()])); | 160 throw new FieldException(t('Attempt to purge a field storage @field_name that still has fields.', ['@field_name' => $field_storage->getName()])); |
165 } | 161 } |
166 | 162 |
167 $state = \Drupal::state(); | 163 $deleted_fields_repository->removeFieldStorageDefinition($field_storage); |
168 $deleted_storages = $state->get('field.storage.deleted'); | |
169 unset($deleted_storages[$field_storage->uuid()]); | |
170 $state->set('field.storage.deleted', $deleted_storages); | |
171 | 164 |
172 // Notify the storage layer. | 165 // Notify the storage layer. |
173 \Drupal::entityManager()->getStorage($field_storage->getTargetEntityTypeId())->finalizePurge($field_storage); | 166 \Drupal::entityManager()->getStorage($field_storage->getTargetEntityTypeId())->finalizePurge($field_storage); |
174 | 167 |
175 // Invoke external hooks after the cache is cleared for API consistency. | 168 // Invoke external hooks after the cache is cleared for API consistency. |