Unverified Commit 7ca98bac authored by alexpott's avatar alexpott

Issue #2939645 by quietone, hgoto, dhirendra.mishra, jungle, kostyashupenko,...

Issue #2939645 by quietone, hgoto, dhirendra.mishra, jungle, kostyashupenko, govind.maloo, John Cook, alexpott: The sample batch finished callback function should have the fourth parameter
parent 97f1ca56
......@@ -651,7 +651,9 @@ function template_preprocess_form_element_label(&$variables) {
*
* Sample callback_batch_finished():
* @code
* function my_finished_callback($success, $results, $operations) {
* function my_finished_callback($success, $results, $operations, $elapsed) {
* // See callback_batch_finished() for more information about these
* // parameters.
* // The 'success' parameter means no fatal PHP errors were detected. All
* // other error management should be handled using 'results'.
* if ($success) {
......
......@@ -109,12 +109,16 @@ function callback_batch_operation($multiple_params, &$context) {
* The value set in $context['results'] by callback_batch_operation().
* @param $operations
* If $success is FALSE, contains the operations that remained unprocessed.
* @param string $elapsed
* A string representing the elapsed time for the batch process, e.g.,
* '1 min 30 secs'.
*/
function callback_batch_finished($success, $results, $operations) {
function callback_batch_finished($success, $results, $operations, $elapsed) {
if ($success) {
// Here we do something meaningful with the results.
$message = t("@count items were processed.", [
$message = t("@count items were processed (@elapsed).", [
'@count' => count($results),
'@elapsed' => $elapsed,
]);
$list = [
'#theme' => 'item_list',
......
......@@ -122,7 +122,7 @@ function _batch_test_nested_batch_callback(array $batches = []) {
/**
* Provides a common 'finished' callback for batches 1 to 7.
*/
function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
function _batch_test_finished_helper($batch_id, $success, $results, $operations, $elapsed) {
if ($results) {
foreach ($results as $op => $op_results) {
$messages[] = 'op ' . Html::escape($op) . ': processed ' . count($op_results) . ' elements';
......@@ -141,9 +141,10 @@ function _batch_test_finished_helper($batch_id, $success, $results, $operations)
// Use item list template to render the messages.
$error_message = [
'#type' => 'inline_template',
'#template' => 'results for batch {{ batch_id }}{{ errors }}',
'#template' => 'results for batch {{ batch_id }} ({{ elapsed }}){{ errors }}',
'#context' => [
'batch_id' => $batch_id,
'elapsed' => $elapsed,
'errors' => [
'#theme' => 'item_list',
'#items' => $messages,
......@@ -152,6 +153,8 @@ function _batch_test_finished_helper($batch_id, $success, $results, $operations)
];
\Drupal::messenger()->addStatus(\Drupal::service('renderer')->renderPlain($error_message));
\Drupal::messenger()->addMessage('elapsed time: ' . $elapsed);
}
/**
......@@ -159,8 +162,8 @@ function _batch_test_finished_helper($batch_id, $success, $results, $operations)
*
* Triggers 'finished' callback for batch 0.
*/
function _batch_test_finished_0($success, $results, $operations) {
_batch_test_finished_helper(0, $success, $results, $operations);
function _batch_test_finished_0($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(0, $success, $results, $operations, $elapsed);
}
/**
......@@ -168,8 +171,8 @@ function _batch_test_finished_0($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 1.
*/
function _batch_test_finished_1($success, $results, $operations) {
_batch_test_finished_helper(1, $success, $results, $operations);
function _batch_test_finished_1($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(1, $success, $results, $operations, $elapsed);
}
/**
......@@ -177,8 +180,8 @@ function _batch_test_finished_1($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 1.
*/
function _batch_test_finished_1_finished($success, $results, $operations) {
_batch_test_finished_helper(1, $success, $results, $operations);
function _batch_test_finished_1_finished($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(1, $success, $results, $operations, $elapsed);
return new RedirectResponse(Url::fromRoute('test_page_test.test_page', [], ['absolute' => TRUE])->toString());
}
......@@ -187,8 +190,8 @@ function _batch_test_finished_1_finished($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 2.
*/
function _batch_test_finished_2($success, $results, $operations) {
_batch_test_finished_helper(2, $success, $results, $operations);
function _batch_test_finished_2($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(2, $success, $results, $operations, $elapsed);
}
/**
......@@ -196,8 +199,8 @@ function _batch_test_finished_2($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 3.
*/
function _batch_test_finished_3($success, $results, $operations) {
_batch_test_finished_helper(3, $success, $results, $operations);
function _batch_test_finished_3($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(3, $success, $results, $operations, $elapsed);
}
/**
......@@ -205,8 +208,8 @@ function _batch_test_finished_3($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 4.
*/
function _batch_test_finished_4($success, $results, $operations) {
_batch_test_finished_helper(4, $success, $results, $operations);
function _batch_test_finished_4($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(4, $success, $results, $operations, $elapsed);
}
/**
......@@ -214,8 +217,8 @@ function _batch_test_finished_4($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 5.
*/
function _batch_test_finished_5($success, $results, $operations) {
_batch_test_finished_helper(5, $success, $results, $operations);
function _batch_test_finished_5($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(5, $success, $results, $operations, $elapsed);
}
/**
......@@ -223,8 +226,8 @@ function _batch_test_finished_5($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 6.
*/
function _batch_test_finished_6($success, $results, $operations) {
_batch_test_finished_helper(6, $success, $results, $operations);
function _batch_test_finished_6($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(6, $success, $results, $operations, $elapsed);
}
/**
......@@ -232,6 +235,6 @@ function _batch_test_finished_6($success, $results, $operations) {
*
* Triggers 'finished' callback for batch 7.
*/
function _batch_test_finished_7($success, $results, $operations) {
_batch_test_finished_helper(7, $success, $results, $operations);
function _batch_test_finished_7($success, $results, $operations, $elapsed) {
_batch_test_finished_helper(7, $success, $results, $operations, $elapsed);
}
......@@ -316,38 +316,40 @@ public function _resultStack($id, $value = 0) {
public function _resultMessages($id) {
$messages = [];
// The elapsed time should be either in minutes and seconds or only seconds.
$pattern_elapsed = ' \((\d+ mins? )?\d+ secs?\)';
switch ($id) {
case 'batch_0':
$messages[] = 'results for batch 0<div class="item-list"><ul><li>none</li></ul></div>';
$messages[] = 'results for batch 0' . $pattern_elapsed . '<div class="item-list"><ul><li>none</li></ul></div>';
break;
case 'batch_1':
$messages[] = 'results for batch 1<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 1' . $pattern_elapsed . '<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
break;
case 'batch_2':
$messages[] = 'results for batch 2<div class="item-list"><ul><li>op 2: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 2' . $pattern_elapsed . '<div class="item-list"><ul><li>op 2: processed 10 elements</li></ul></div>';
break;
case 'batch_3':
$messages[] = 'results for batch 3<div class="item-list"><ul><li>op 1: processed 10 elements</li><li>op 2: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 3' . $pattern_elapsed . '<div class="item-list"><ul><li>op 1: processed 10 elements</li><li>op 2: processed 10 elements</li></ul></div>';
break;
case 'batch_4':
$messages[] = 'results for batch 4<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 4' . $pattern_elapsed . '<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
$messages = array_merge($messages, $this->_resultMessages('batch_2'));
break;
case 'batch_5':
$messages[] = 'results for batch 5<div class="item-list"><ul><li>op 5: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 5' . $pattern_elapsed . '<div class="item-list"><ul><li>op 5: processed 10 elements</li></ul></div>';
break;
case 'batch_6':
$messages[] = 'results for batch 6<div class="item-list"><ul><li>op 6: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 6' . $pattern_elapsed . '<div class="item-list"><ul><li>op 6: processed 10 elements</li></ul></div>';
break;
case 'batch_7':
$messages[] = 'results for batch 7<div class="item-list"><ul><li>op 7: processed 10 elements</li></ul></div>';
$messages[] = 'results for batch 7' . $pattern_elapsed . '<div class="item-list"><ul><li>op 7: processed 10 elements</li></ul></div>';
$messages = array_merge($messages, $this->_resultMessages('batch_6'));
$messages = array_merge($messages, $this->_resultMessages('batch_5'));
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment