3 namespace Drupal\Core\Config\Importer;
5 use Drupal\Core\Config\ConfigImporter;
8 * Methods for running the ConfigImporter in a batch.
10 * @see \Drupal\Core\Config\ConfigImporter
12 class ConfigImporterBatch {
15 * Processes the config import batch and persists the importer.
17 * @param \Drupal\Core\Config\ConfigImporter $config_importer
18 * The batch config importer object to persist.
19 * @param string $sync_step
20 * The synchronization step to do.
21 * @param array $context
24 public static function process(ConfigImporter $config_importer, $sync_step, &$context) {
25 if (!isset($context['sandbox']['config_importer'])) {
26 $context['sandbox']['config_importer'] = $config_importer;
29 $config_importer = $context['sandbox']['config_importer'];
30 $config_importer->doSyncStep($sync_step, $context);
31 if ($errors = $config_importer->getErrors()) {
32 if (!isset($context['results']['errors'])) {
33 $context['results']['errors'] = [];
35 $context['results']['errors'] = array_merge($errors, $context['results']['errors']);
42 * This function is a static function to avoid serializing the ConfigSync
43 * object unnecessarily.
45 * @param bool $success
46 * Indicate that the batch API tasks were all completed successfully.
47 * @param array $results
48 * An array of all the results that were updated in update_do_one().
49 * @param array $operations
50 * A list of the operations that had not been completed by the batch API.
52 public static function finish($success, $results, $operations) {
53 $messenger = \Drupal::messenger();
55 if (!empty($results['errors'])) {
56 $logger = \Drupal::logger('config_sync');
57 foreach ($results['errors'] as $error) {
58 $messenger->addError($error);
59 $logger->error($error);
61 $messenger->addWarning(t('The configuration was imported with errors.'));
63 elseif (!drupal_installation_attempted()) {
64 // Display a success message when not installing Drupal.
65 $messenger->addStatus(t('The configuration was imported successfully.'));
70 // $operations contains the operations that remained unprocessed.
71 $error_operation = reset($operations);
72 $message = t('An error occurred while processing %error_operation with arguments: @arguments', ['%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)]);
73 $messenger->addError($message);