diff --git a/core/lib/Drupal/Core/Batch/Batch.php b/core/lib/Drupal/Core/Batch/Batch.php index 6c124fd..0c6b340 100644 --- a/core/lib/Drupal/Core/Batch/Batch.php +++ b/core/lib/Drupal/Core/Batch/Batch.php @@ -41,21 +41,21 @@ class Batch { * * @var string */ - protected $init_message; + protected $initMessage; /** * The message to be shown while the batch is in progress. * * @var string */ - protected $progress_message; + protected $progressMessage; /** * The message to be shown if a problem occurs. * * @var string */ - protected $error_message; + protected $errorMessage; /** * The name of a function / method to be called when the batch finishes. @@ -86,7 +86,7 @@ class Batch { * * @var array */ - protected $url_options = []; + protected $urlOptions = []; /** * Specifies if the batch is progressive. @@ -150,10 +150,65 @@ public static function createFromTitle($title) { * A new initialized object. */ public static function createFromArray(array $batch_definition) { - $new_batch = new static(isset($batch_definition['title']) ? $batch_definition['title'] : NULL); + $new_batch = self::create(); - foreach ($batch_definition as $key => $value) { - $new_batch->{$key} = $value; + if (isset($batch_definition['operations'])) { + foreach ($batch_definition['operations'] as list($callback, $arguments)) { + $new_batch->addOperation($callback, $arguments); + } + } + + if (isset($batch_definition['title'])) { + $new_batch->setTitle($batch_definition['title']); + } + + if (isset($batch_definition['init_message'])) { + $new_batch->setInitMessage($batch_definition['init_message']); + } + + if (isset($batch_definition['progress_message'])) { + $new_batch->setProgressMessage($batch_definition['progress_message']); + } + + if (isset($batch_definition['error_message'])) { + $new_batch->setErrorMessage($batch_definition['error_message']); + } + + if (isset($batch_definition['finished'])) { + $new_batch->setFinishCallback($batch_definition['finished']); + } + + if (isset($batch_definition['file'])) { + $new_batch->setFile($batch_definition['file']); + } + + if (isset($batch_definition['css'])) { + if (is_array($batch_definition['css'])) { + $new_batch->setCss($batch_definition['css']); + } + else { + $new_batch->addCss($batch_definition['css']); + } + } + + if (isset($batch_definition['url_options'])) { + $new_batch->setUrlOptions($batch_definition['url_options']); + } + + if (isset($batch_definition['progressive'])) { + $new_batch->setProgressive($batch_definition['progressive']); + } + + if (isset($batch_definition['queue'])) { + if ( + isset($batch_definition['queue']['name']) && + isset($batch_definition['queue']['class']) + ) { + $new_batch->setQueue( + $batch_definition['queue']['name'], + $batch_definition['queue']['class'] + ); + } } return $new_batch; @@ -196,7 +251,7 @@ public function setFinishCallback($callback) { * @return $this */ public function setInitMessage($message) { - $this->init_message = $message; + $this->initMessage = $message; return $this; } @@ -211,7 +266,7 @@ public function setInitMessage($message) { * @return $this */ public function setProgressMessage($message) { - $this->progress_message = $message; + $this->progressMessage = $message; return $this; } @@ -224,7 +279,7 @@ public function setProgressMessage($message) { * @return $this */ public function setErrorMessage($message) { - $this->error_message = $message; + $this->errorMessage = $message; return $this; } @@ -271,7 +326,7 @@ public function setCss(array $filenames) { * @see \Drupal\Core\Url */ public function setUrlOptions(array $options = []) { - $this->url_options = $options; + $this->urlOptions = $options; return $this; } @@ -368,20 +423,19 @@ public function enqueue() { * The array representation of the object. */ public function toArray() { - $array = []; - foreach ($this as $key => $value) { - if (!is_null($value)) { - $array[$key] = $value; - } - else { - switch ($key) { - case 'operations': - case 'css': - case 'url_options': - $array[$key] = []; - } - } - } + $array = [ + 'operations' => $this->operations ?: [], + 'title' => $this->title ?: '', + 'init_message' => $this->initMessage ?: '', + 'progress_message' => $this->progressMessage ?: '', + 'error_message' => $this->errorMessage ?: '', + 'finished' => $this->finished, + 'file' => $this->file, + 'css' => $this->css ?: [], + 'url_options' => $this->urlOptions ?: [], + 'progressive' => $this->progressive, + 'queue' => $this->queue ?: NULL, + ]; return $array; } diff --git a/core/lib/Drupal/Core/Batch/BatchQueueController.php b/core/lib/Drupal/Core/Batch/BatchQueueController.php index 1c4e650..046da09 100644 --- a/core/lib/Drupal/Core/Batch/BatchQueueController.php +++ b/core/lib/Drupal/Core/Batch/BatchQueueController.php @@ -7,7 +7,7 @@ use Drupal\Core\Database\Database; use Drupal\Core\Form\FormState; use Drupal\Core\Queue\Batch as BatchQueue; -use Drupal\Core\BatchMemory; +use Drupal\Core\Queue\BatchMemory; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -575,14 +575,14 @@ protected static function populateQueue($set_id = 0) { $batch_set = &$batch['sets'][$set_id]; if (isset($batch_set['operations'])) { - $batch_set += array( - 'queue' => [ + if (empty($batch_set['queue'])) { + $batch_set['queue'] = [ 'name' => 'drupal_batch:' . $batch['id'] . ':' . $set_id, 'class' => ( - $batch['progressive'] ? BatchQueue::class : BatchMemory::class + $batch['progressive'] ? BatchQueue::class : BatchMemory::class ), - ], - ); + ]; + } $queue = self::getQueueForBatch($batch_set); $queue->createQueue();