function 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.