You are here

protected static function ConfigImporterFieldPurger::initializeSandbox in Drupal 9

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

Initializes the batch context sandbox for processing field deletions.

This calculates the number of steps necessary to purge all the field data and saves data for later use.

Parameters

array $context: The batch context.

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

1 call to ConfigImporterFieldPurger::initializeSandbox()
ConfigImporterFieldPurger::process in core/modules/field/src/ConfigImporterFieldPurger.php
Processes fields targeted for purge as part of a configuration sync.

File

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

Class

ConfigImporterFieldPurger
Processes field purges before a configuration synchronization.

Namespace

Drupal\field

Code

protected static function initializeSandbox(array &$context, ConfigImporter $config_importer) {
  $context['sandbox']['field']['purge_batch_size'] = \Drupal::config('field.settings')
    ->get('purge_batch_size');

  // Save the future list of installed extensions to limit the amount of times
  // the configuration is read from disk.
  $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) {

      // The number of steps to delete each field is determined by the
      // purge_batch_size setting. For example if the field has 9 rows and the
      // batch size is 10 then this will add 1 step to $number_of_steps.
      $how_many_steps = ceil($row_count / $context['sandbox']['field']['purge_batch_size']);
      $context['sandbox']['field']['steps_to_delete'] += $how_many_steps;
    }
  }

  // Each field possibly needs one last field_purge_batch() call to remove the
  // last field and the field storage itself.
  $context['sandbox']['field']['steps_to_delete'] += count($fields);
  $context['sandbox']['field']['current_progress'] = 0;
}