You are here

public static function ConfigImporterFieldPurger::process in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/src/ConfigImporterFieldPurger.php \Drupal\field\ConfigImporterFieldPurger::process()

Processes fields targeted for purge as part of a configuration sync.

This takes care of deleting the field if necessary, and purging the data on the fly.

Parameters

array $context: The batch context.

\Drupal\Core\Config\ConfigImporter $config_importer: The config importer.

File

core/modules/field/src/ConfigImporterFieldPurger.php, line 25

Class

ConfigImporterFieldPurger
Processes field purges before a configuration synchronization.

Namespace

Drupal\field

Code

public static function process(array &$context, ConfigImporter $config_importer) {
  if (!isset($context['sandbox']['field'])) {
    static::initializeSandbox($context, $config_importer);
  }

  // Get the list of field storages to purge.
  $field_storages = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions'], $config_importer
    ->getUnprocessedConfiguration('delete'));

  // Get the first field storage to process.
  $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 the storage has not been deleted yet we need to do that. This is the
    // case when the storage deletion is staged.
    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(),
    ]);
  }
}