Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Config / Importer / ConfigImporterBatch.php
1 <?php
2
3 namespace Drupal\Core\Config\Importer;
4
5 use Drupal\Core\Config\ConfigImporter;
6
7 /**
8  * Methods for running the ConfigImporter in a batch.
9  *
10  * @see \Drupal\Core\Config\ConfigImporter
11  */
12 class ConfigImporterBatch {
13
14   /**
15    * Processes the config import batch and persists the importer.
16    *
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
22    *   The batch context.
23    */
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;
27     }
28
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'] = [];
34       }
35       $context['results']['errors'] = array_merge($errors, $context['results']['errors']);
36     }
37   }
38
39   /**
40    * Finish batch.
41    *
42    * This function is a static function to avoid serializing the ConfigSync
43    * object unnecessarily.
44    *
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.
51    */
52   public static function finish($success, $results, $operations) {
53     $messenger = \Drupal::messenger();
54     if ($success) {
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);
60         }
61         $messenger->addWarning(t('The configuration was imported with errors.'));
62       }
63       elseif (!drupal_installation_attempted()) {
64         // Display a success message when not installing Drupal.
65         $messenger->addStatus(t('The configuration was imported successfully.'));
66       }
67     }
68     else {
69       // An error occurred.
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);
74     }
75   }
76
77 }