function field_purge_batch in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/field.purge.inc \field_purge_batch()
Purges a batch of deleted Field API data, field storages, or fields.
This function will purge deleted field data in batches. The batch size is defined as an argument to the function, and once each batch is finished, it continues with the next batch until all have completed. If a deleted field with no remaining data records is found, the field itself will be purged. If a deleted field storage with no remaining fields is found, the field storage itself will be purged.
Parameters
$batch_size: The maximum number of field data records to purge before returning.
string $field_storage_uuid: (optional) Limit the purge to a specific field storage.
Related topics
11 calls to field_purge_batch()
- BulkDeleteTest::testPurgeField in core/
modules/ field/ src/ Tests/ BulkDeleteTest.php - Verify that field data items and fields are purged when a field storage is deleted.
- BulkDeleteTest::testPurgeFieldStorage in core/
modules/ field/ src/ Tests/ BulkDeleteTest.php - Verify that field storages are preserved and purged correctly as multiple fields are deleted and purged.
- CommentFieldsTest::testCommentInstallAfterContentModule in core/
modules/ comment/ src/ Tests/ CommentFieldsTest.php - Tests that comment module works when installed after a content module.
- CommentUninstallTest::testCommentUninstallWithoutField in core/
modules/ comment/ src/ Tests/ CommentUninstallTest.php - Tests if uninstallation succeeds if the field has been deleted beforehand.
- ConfigImportAllTest::testInstallUninstall in core/
modules/ config/ src/ Tests/ ConfigImportAllTest.php - Tests that a fixed set of modules can be installed and uninstalled.
File
- core/
modules/ field/ field.purge.inc, line 75 - Provides support for field data purge after mass deletion.
Code
function field_purge_batch($batch_size, $field_storage_uuid = NULL) {
$properties = array(
'deleted' => TRUE,
'include_deleted' => TRUE,
);
if ($field_storage_uuid) {
$properties['field_storage_uuid'] = $field_storage_uuid;
}
$fields = entity_load_multiple_by_properties('field_config', $properties);
$info = \Drupal::entityManager()
->getDefinitions();
foreach ($fields as $field) {
$entity_type = $field
->getTargetEntityTypeId();
// We cannot purge anything if the entity type is unknown (e.g. the
// providing module was uninstalled).
// @todo Revisit after https://www.drupal.org/node/2080823.
if (!isset($info[$entity_type])) {
continue;
}
$count_purged = \Drupal::entityManager()
->getStorage($entity_type)
->purgeFieldData($field, $batch_size);
if ($count_purged < $batch_size || $count_purged == 0) {
// No field data remains for the field, so we can remove it.
field_purge_field($field);
}
$batch_size -= $count_purged;
// Only delete up to the maximum number of records.
if ($batch_size == 0) {
break;
}
}
// Retrieve all deleted field storages. Any that have no fields can be purged.
$deleted_storages = \Drupal::state()
->get('field.storage.deleted') ?: array();
foreach ($deleted_storages as $field_storage) {
$field_storage = new FieldStorageConfig($field_storage);
if ($field_storage_uuid && $field_storage
->uuid() != $field_storage_uuid) {
// If a specific UUID is provided, only purge the corresponding field.
continue;
}
// We cannot purge anything if the entity type is unknown (e.g. the
// providing module was uninstalled).
// @todo Revisit after https://www.drupal.org/node/2080823.
if (!isset($info[$field_storage
->getTargetEntityTypeId()])) {
continue;
}
$fields = entity_load_multiple_by_properties('field_config', array(
'field_storage_uuid' => $field_storage
->uuid(),
'include_deleted' => TRUE,
));
if (empty($fields)) {
field_purge_field_storage($field_storage);
}
}
}