ConfigImporterFieldPurger.php in Drupal 10
File
core/modules/field/src/ConfigImporterFieldPurger.php
View source
<?php
namespace Drupal\field;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\field\Entity\FieldStorageConfig;
class ConfigImporterFieldPurger {
public static function process(array &$context, ConfigImporter $config_importer) {
if (!isset($context['sandbox']['field'])) {
static::initializeSandbox($context, $config_importer);
}
$field_storages = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions'], $config_importer
->getUnprocessedConfiguration('delete'));
$field_storage = reset($field_storages);
if (!isset($context['sandbox']['field']['current_storage_id']) || $context['sandbox']['field']['current_storage_id'] != $field_storage
->id()) {
$context['sandbox']['field']['current_storage_id'] = $field_storage
->id();
if (!$field_storage
->isDeleted()) {
$field_storage
->delete();
}
}
field_purge_batch($context['sandbox']['field']['purge_batch_size'], $field_storage
->getUniqueStorageIdentifier());
$context['sandbox']['field']['current_progress']++;
$fields_to_delete_count = count(static::getFieldStoragesToPurge($context['sandbox']['field']['extensions'], $config_importer
->getUnprocessedConfiguration('delete')));
if ($fields_to_delete_count == 0) {
$context['finished'] = 1;
}
else {
$context['finished'] = $context['sandbox']['field']['current_progress'] / $context['sandbox']['field']['steps_to_delete'];
$context['message'] = \Drupal::translation()
->translate('Purging field @field_label', [
'@field_label' => $field_storage
->label(),
]);
}
}
protected static function initializeSandbox(array &$context, ConfigImporter $config_importer) {
$context['sandbox']['field']['purge_batch_size'] = \Drupal::config('field.settings')
->get('purge_batch_size');
$context['sandbox']['field']['extensions'] = $config_importer
->getStorageComparer()
->getSourceStorage()
->read('core.extension');
$context['sandbox']['field']['steps_to_delete'] = 0;
$fields = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions'], $config_importer
->getUnprocessedConfiguration('delete'));
foreach ($fields as $field) {
$row_count = \Drupal::entityTypeManager()
->getStorage($field
->getTargetEntityTypeId())
->countFieldData($field);
if ($row_count > 0) {
$how_many_steps = ceil($row_count / $context['sandbox']['field']['purge_batch_size']);
$context['sandbox']['field']['steps_to_delete'] += $how_many_steps;
}
}
$context['sandbox']['field']['steps_to_delete'] += count($fields);
$context['sandbox']['field']['current_progress'] = 0;
}
public static function getFieldStoragesToPurge(array $extensions, array $deletes) {
$providers = array_keys($extensions['module']);
$providers[] = 'core';
$storages_to_delete = [];
$field_storage_ids = [];
foreach ($deletes as $config_name) {
$field_storage_config_prefix = \Drupal::entityTypeManager()
->getDefinition('field_storage_config')
->getConfigPrefix();
if (strpos($config_name, $field_storage_config_prefix . '.') === 0) {
$field_storage_ids[] = ConfigEntityStorage::getIDFromConfigName($config_name, $field_storage_config_prefix);
}
}
if (!empty($field_storage_ids)) {
$field_storages = \Drupal::entityQuery('field_storage_config')
->condition('id', $field_storage_ids, 'IN')
->condition('module', $providers, 'NOT IN')
->execute();
if (!empty($field_storages)) {
$storages_to_delete = FieldStorageConfig::loadMultiple($field_storages);
}
}
$deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')
->getFieldStorageDefinitions();
foreach ($deleted_storage_definitions as $field_storage_definition) {
if ($field_storage_definition instanceof FieldStorageConfigInterface && !in_array($field_storage_definition
->getTypeProvider(), $providers)) {
$storages_to_delete[$field_storage_definition
->id()] = $field_storage_definition;
}
}
return $storages_to_delete;
}
}