class FieldPurger
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/FieldPurger.php \Drupal\Core\Field\FieldPurger
Hierarchy
- class \Drupal\Core\Field\FieldPurger
Expanded class hierarchy of FieldPurger
Related topics
12 files declare their use of FieldPurger
- BulkDeleteTest.php in core/
modules/ field/ tests/ src/ Kernel/ BulkDeleteTest.php - CommentUninstallTest.php in core/
modules/ comment/ tests/ src/ Kernel/ CommentUninstallTest.php - ConfigImportAllTest.php in core/
modules/ config/ tests/ src/ Functional/ ConfigImportAllTest.php - ConfigImporterFieldPurger.php in core/
modules/ field/ src/ ConfigImporterFieldPurger.php - EntityBundleFieldTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityBundleFieldTest.php
File
-
core/
lib/ Drupal/ Core/ Field/ FieldPurger.php, line 59
Namespace
Drupal\Core\FieldView source
class FieldPurger {
public function __construct(protected EntityTypeManagerInterface $entityTypeManager, protected DeletedFieldsRepositoryInterface $deletedFieldsRepository, protected ModuleHandlerInterface $moduleHandler, protected LoggerChannelFactoryInterface $loggerFactory) {
}
/**
* Purges a batch of deleted entity field 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.
*
* @param int $batch_size
* The maximum number of field data records to purge before returning.
* @param string|null $field_storage_unique_id
* (optional) Limit the purge to a specific field storage. Defaults to NULL.
*/
public function purgeBatch(int $batch_size, string|null $field_storage_unique_id = NULL) : void {
$fields = $this->deletedFieldsRepository
->getFieldDefinitions($field_storage_unique_id);
$info = $this->entityTypeManager
->getDefinitions();
foreach ($fields as $field) {
$entity_type_id = $field->getTargetEntityTypeId();
// We cannot purge anything if the entity type is unknown (e.g., the
// providing module was uninstalled).
if (!isset($info[$entity_type_id])) {
$this->loggerFactory
->get('field')
->warning('Cannot remove field @field_name because the entity type is unknown: %entity_type_id', [
'@field_name' => $field->getName(),
'%entity_type_id' => $entity_type_id,
]);
continue;
}
$count_purged = $this->entityTypeManager
->getStorage($entity_type_id)
->purgeFieldData($field, $batch_size);
if ($count_purged < $batch_size || $count_purged === 0) {
// No field data remains for the field, so we can remove it.
$this->purgeFieldDefinition($field);
}
$batch_size -= $count_purged;
// Only delete up to the maximum number of records.
if ($batch_size == 0) {
break;
}
}
// Retrieve all deleted field storage definitions. Any that have no fields
// can be purged.
foreach ($this->deletedFieldsRepository
->getFieldStorageDefinitions() as $field_storage) {
if ($field_storage_unique_id && $field_storage->getUniqueStorageIdentifier() != $field_storage_unique_id) {
// 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).
$storage_entity_type_id = $field_storage->getTargetEntityTypeId();
if (!isset($info[$storage_entity_type_id])) {
$this->loggerFactory
->get('field')
->warning('Cannot remove field_storage @field_storage_name because the entity type is unknown: %storage_entity_type_id', [
'@field_storage_name' => $field_storage->getName(),
'%storage_entity_type_id' => $storage_entity_type_id,
]);
continue;
}
$fields = $this->deletedFieldsRepository
->getFieldDefinitions($field_storage->getUniqueStorageIdentifier());
if (empty($fields)) {
$this->purgeFieldStorageDefinition($field_storage);
}
}
}
/**
* @} End of "defgroup field_purge".
*/
/**
* Purges a field definition from the database.
*
* This function assumes all data for the field has already been purged and
* should only be called by purgeBatch().
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field
* The field definition to purge.
*/
protected function purgeFieldDefinition(FieldDefinitionInterface $field) : void {
$this->deletedFieldsRepository
->removeFieldDefinition($field);
// Invoke external hooks after the cache is cleared for API consistency.
$this->moduleHandler
->invokeAll('field_purge_field', [
$field,
]);
}
/**
* Purges a field storage definition from the database.
*
* This function assumes all fields for the field storage has already been
* purged, and should only be called by purgeBatch().
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage
* The field storage definition to purge.
*
* @throws \Drupal\Core\Field\FieldException
*/
protected function purgeFieldStorageDefinition(FieldStorageDefinitionInterface $field_storage) : void {
$fields = $this->deletedFieldsRepository
->getFieldDefinitions($field_storage->getUniqueStorageIdentifier());
if (count($fields) > 0) {
throw new FieldException("Attempt to purge a field storage {$field_storage->getName()} that still has fields.");
}
$this->deletedFieldsRepository
->removeFieldStorageDefinition($field_storage);
// Notify the storage layer.
$this->entityTypeManager
->getStorage($field_storage->getTargetEntityTypeId())
->finalizePurge($field_storage);
// Invoke external hooks after the cache is cleared for API consistency.
$this->moduleHandler
->invokeAll('field_purge_field_storage', [
$field_storage,
]);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.