diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 159d729b9e..875b94967d 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -9,6 +9,7 @@ use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Config\ConfigImporterException; +use Drupal\Core\Config\Importer\ConfigImporterBatch; use Drupal\Core\Config\StorageComparer; use Drupal\Core\DrupalKernel; use Drupal\Core\Database\Database; @@ -2341,13 +2342,13 @@ function install_config_import_batch() { $batch_builder = new BatchBuilder(); $batch_builder - ->setFinishCallback('install_config_import_batch_finish') + ->setFinishCallback([ConfigImporterBatch::class, 'finish']) ->setTitle(t('Importing configuration')) ->setInitMessage(t('Starting configuration import.')) ->setErrorMessage(t('Configuration import has encountered an error.')); foreach ($sync_steps as $sync_step) { - $batch_builder->addOperation('install_config_import_batch_process', [$config_importer, $sync_step]); + $batch_builder->addOperation([ConfigImporterBatch::class, 'process'], [$config_importer, $sync_step]); } return $batch_builder->toArray(); @@ -2364,62 +2365,6 @@ function install_config_import_batch() { } } -/** - * 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 $context - * The batch context. - * - * @see install_config_import_batch() - */ -function install_config_import_batch_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($context['results']['errors'], $errors); - } -} - -/** - * Finish config importer batch. - * - * @see install_config_import_batch() - */ -function install_config_import_batch_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.')); - } - } - 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); - } -} - /** * Replaces install_download_translation() during configuration installs. * diff --git a/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php b/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php new file mode 100644 index 0000000000..8aee289e0d --- /dev/null +++ b/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php @@ -0,0 +1,77 @@ +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 (!drupal_installation_attempted()) { + // 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); + } + } + +} diff --git a/core/modules/config/src/Form/ConfigSync.php b/core/modules/config/src/Form/ConfigSync.php index 44034ddabb..57fb1d860e 100644 --- a/core/modules/config/src/Form/ConfigSync.php +++ b/core/modules/config/src/Form/ConfigSync.php @@ -4,6 +4,7 @@ use Drupal\Core\Config\ConfigImporterException; use Drupal\Core\Config\ConfigImporter; +use Drupal\Core\Config\Importer\ConfigImporterBatch; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleInstallerInterface; @@ -337,14 +338,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $sync_steps = $config_importer->initialize(); $batch = [ 'operations' => [], - 'finished' => [get_class($this), 'finishBatch'], + 'finished' => [ConfigImporterBatch::class, 'finish'], 'title' => t('Synchronizing configuration'), 'init_message' => t('Starting configuration synchronization.'), 'progress_message' => t('Completed step @current of @total.'), 'error_message' => t('Configuration synchronization has encountered an error.'), ]; foreach ($sync_steps as $sync_step) { - $batch['operations'][] = [[get_class($this), 'processBatch'], [$config_importer, $sync_step]]; + $batch['operations'][] = [[ConfigImporterBatch::class, 'process'], [$config_importer, $sync_step]]; } batch_set($batch); @@ -368,20 +369,15 @@ public function submitForm(array &$form, FormStateInterface $form_state) { * The synchronization step to do. * @param array $context * The batch context. + * + * @deprecated in Drupal 8.6.0 and will be removed before 9.0.0. Use + * \Drupal\Core\Config\Importer\ConfigImporterBatch::process() instead. + * + * @see https://www.drupal.org/node/2897299 */ public static function processBatch(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'] += $errors; - } + @trigger_error('\Drupal\config\Form\ConfigSync::processBatch() deprecated in Drupal 8.6.0 and will be removed before 9.0.0. Use \Drupal\Core\Config\Importer\ConfigImporterBatch::process() instead. See https://www.drupal.org/node/2897299'); + ConfigImporterBatch::process($config_importer, $sync_step, $context); } /** @@ -389,27 +385,15 @@ public static function processBatch(ConfigImporter $config_importer, $sync_step, * * This function is a static function to avoid serializing the ConfigSync * object unnecessarily. + * + * @deprecated in Drupal 8.6.0 and will be removed before 9.0.0. Use + * \Drupal\Core\Config\Importer\ConfigImporterBatch::finish() instead. + * + * @see https://www.drupal.org/node/2897299 */ public static function finishBatch($success, $results, $operations) { - if ($success) { - if (!empty($results['errors'])) { - foreach ($results['errors'] as $error) { - \Drupal::messenger()->addError($error); - \Drupal::logger('config_sync')->error($error); - } - \Drupal::messenger()->addWarning(\Drupal::translation()->translate('The configuration was imported with errors.')); - } - else { - \Drupal::messenger()->addStatus(\Drupal::translation()->translate('The configuration was imported successfully.')); - } - } - else { - // An error occurred. - // $operations contains the operations that remained unprocessed. - $error_operation = reset($operations); - $message = \Drupal::translation()->translate('An error occurred while processing %error_operation with arguments: @arguments', ['%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)]); - \Drupal::messenger()->addError($message); - } + @trigger_error('\Drupal\config\Form\ConfigSync::finishBatch() deprecated in Drupal 8.6.0 and will be removed before 9.0.0. Use \Drupal\Core\Config\Importer\ConfigImporterBatch::finish() instead. See https://www.drupal.org/node/2897299'); + ConfigImporterBatch::finish($success, $results, $operations); } }