You are here

class ConfigImporterBatch in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php \Drupal\Core\Config\Importer\ConfigImporterBatch
  2. 9 core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php \Drupal\Core\Config\Importer\ConfigImporterBatch

Methods for running the ConfigImporter in a batch.

Hierarchy

Expanded class hierarchy of ConfigImporterBatch

See also

\Drupal\Core\Config\ConfigImporter

3 files declare their use of ConfigImporterBatch
ConfigSingleImportForm.php in core/modules/config/src/Form/ConfigSingleImportForm.php
ConfigSync.php in core/modules/config/src/Form/ConfigSync.php
install.core.inc in core/includes/install.core.inc
API functions for installing Drupal.

File

core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php, line 13

Namespace

Drupal\Core\Config\Importer
View source
class ConfigImporterBatch {

  /**
   * Processes the config import batch and persists the importer.
   *
   * @param \Drupal\Core\Config\ConfigImporter $config_importer
   *   The batch config importer object to persist.
   * @param string $sync_step
   *   The synchronization step to do.
   * @param array $context
   *   The batch context.
   */
  public static function process(ConfigImporter $config_importer, $sync_step, &$context) {
    if (!isset($context['sandbox']['config_importer'])) {
      $context['sandbox']['config_importer'] = $config_importer;
    }
    $config_importer = $context['sandbox']['config_importer'];
    $config_importer
      ->doSyncStep($sync_step, $context);
    if ($errors = $config_importer
      ->getErrors()) {
      if (!isset($context['results']['errors'])) {
        $context['results']['errors'] = [];
      }
      $context['results']['errors'] = array_merge($errors, $context['results']['errors']);
    }
  }

  /**
   * Finish batch.
   *
   * This function is a static function to avoid serializing the ConfigSync
   * object unnecessarily.
   *
   * @param bool $success
   *   Indicate that the batch API tasks were all completed successfully.
   * @param array $results
   *   An array of all the results that were updated in update_do_one().
   * @param array $operations
   *   A list of the operations that had not been completed by the batch API.
   */
  public static function finish($success, $results, $operations) {
    $messenger = \Drupal::messenger();
    if ($success) {
      if (!empty($results['errors'])) {
        $logger = \Drupal::logger('config_sync');
        foreach ($results['errors'] as $error) {
          $messenger
            ->addError($error);
          $logger
            ->error($error);
        }
        $messenger
          ->addWarning(t('The configuration was imported with errors.'));
      }
      elseif (!InstallerKernel::installationAttempted()) {

        // Display a success message when not installing Drupal.
        $messenger
          ->addStatus(t('The configuration was imported successfully.'));
      }
    }
    else {

      // An error occurred.
      // $operations contains the operations that remained unprocessed.
      $error_operation = reset($operations);
      $message = t('An error occurred while processing %error_operation with arguments: @arguments', [
        '%error_operation' => $error_operation[0],
        '@arguments' => print_r($error_operation[1], TRUE),
      ]);
      $messenger
        ->addError($message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigImporterBatch::finish public static function Finish batch.
ConfigImporterBatch::process public static function Processes the config import batch and persists the importer.