diff --git a/batch_example/batch_example.install b/batch_example/batch_example.install index f26a32f..857b4d7 100644 --- a/batch_example/batch_example.install +++ b/batch_example/batch_example.install @@ -33,7 +33,7 @@ function batch_example_update_8001(&$sandbox) { // Total nodes that must be visited. $sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node}')->fetchField(); // A place to store messages during the run. - $sandbox['messages'] = array(); + $sandbox['messages'] = []; // Last node read via the query. $sandbox['current_node'] = -1; } @@ -46,15 +46,15 @@ function batch_example_update_8001(&$sandbox) { // Retrieve the next group of nids. $query = db_select('node', 'n'); - $query->fields('n', array('nid')); + $query->fields('n', ['nid']); $result = $query - ->where('n.nid > :nid', array(':nid' => $sandbox['current_node'])) + ->where('n.nid > :nid', [':nid' => $sandbox['current_node']]) ->range(0, $limit) ->orderBy('n.nid', 'ASC') ->execute(); foreach ($result as $row) { // Here we actually perform a dummy 'update' on the current node. - $node = db_query('SELECT nid FROM {node} WHERE nid = :nid', array(':nid' => $row->nid))->fetchField(); + $node = db_query('SELECT nid FROM {node} WHERE nid = :nid', [':nid' => $row->nid])->fetchField(); // Update our progress information. $sandbox['progress']++; @@ -78,6 +78,6 @@ function batch_example_update_8001(&$sandbox) { // hook_update_N() may optionally return a string which will be displayed // to the user. $final_message = '"; - return t('The batch_example demonstration update did what it was supposed to do: @message', array('@message' => $final_message)); + return t('The batch_example demonstration update did what it was supposed to do: @message', ['@message' => $final_message]); } } diff --git a/batch_example/batch_example.module b/batch_example/batch_example.module index f047adc..c7873ed 100644 --- a/batch_example/batch_example.module +++ b/batch_example/batch_example.module @@ -44,7 +44,7 @@ function batch_example_op_1($id, $operation_details, &$context) { // Optional message displayed under the progressbar. $context['message'] = t('Running Batch "@id" @details', - array('@id' => $id, '@details' => $operation_details) + ['@id' => $id, '@details' => $operation_details] ); } @@ -60,7 +60,7 @@ function batch_example_op_2($operation_details, &$context) { // Use the $context['sandbox'] at your convenience to store the // information needed to track progression between successive calls. if (empty($context['sandbox'])) { - $context['sandbox'] = array(); + $context['sandbox'] = []; $context['sandbox']['progress'] = 0; $context['sandbox']['current_node'] = 0; @@ -92,7 +92,7 @@ function batch_example_op_2($operation_details, &$context) { $context['sandbox']['progress']++; $context['sandbox']['current_node'] = $row; $context['message'] = t('Running Batch "@id" @details', - array('@id' => $row, '@details' => $operation_details) + ['@id' => $row, '@details' => $operation_details] ); } @@ -110,8 +110,8 @@ function batch_example_finished($success, $results, $operations) { if ($success) { // Here we could do something meaningful with the results. // We just display the number of nodes we processed... - drupal_set_message(t('@count results processed.', array('@count' => count($results)))); - drupal_set_message(t('The final result was "%final"', array('%final' => end($results)))); + drupal_set_message(t('@count results processed.', ['@count' => count($results)])); + drupal_set_message(t('The final result was "%final"', ['%final' => end($results)])); } else { // An error occurred. @@ -119,10 +119,10 @@ function batch_example_finished($success, $results, $operations) { $error_operation = reset($operations); drupal_set_message( t('An error occurred while processing @operation with arguments : @args', - array( + [ '@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE), - ) + ] ) ); } diff --git a/batch_example/src/Form/BatchExampleForm.php b/batch_example/src/Form/BatchExampleForm.php index 7ab2260..476fdda 100644 --- a/batch_example/src/Form/BatchExampleForm.php +++ b/batch_example/src/Form/BatchExampleForm.php @@ -22,22 +22,22 @@ class BatchExampleForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { - $form['description'] = array( + $form['description'] = [ '#type' => 'markup', '#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'), - ); - $form['batch'] = array( + ]; + $form['batch'] = [ '#type' => 'select', '#title' => 'Choose batch', - '#options' => array( + '#options' => [ 'batch_1' => t('batch 1 - 1000 operations'), 'batch_2' => t('batch 2 - 20 operations.'), - ), - ); - $form['submit'] = array( + ], + ]; + $form['submit'] = [ '#type' => 'submit', '#value' => 'Go', - ); + ]; return $form; @@ -50,7 +50,7 @@ class BatchExampleForm extends FormBase { // Gather our form value. $value = $form_state->getValues()['batch']; // Set the batch, using convenience methods. - $batch = array(); + $batch = []; switch ($value) { case 'batch_1': $batch = $this->generateBatch1(); @@ -75,9 +75,9 @@ class BatchExampleForm extends FormBase { */ public function generateBatch1() { $num_operations = 1000; - drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations))); + drupal_set_message(t('Creating an array of @num operations', ['@num' => $num_operations])); - $operations = array(); + $operations = []; // Set up an operations array with 1000 elements, each doing function // batch_example_op_1. // Each operation in the operations array means at least one new HTTP @@ -89,19 +89,19 @@ class BatchExampleForm extends FormBase { // Each operation is an array consisting of // - The function to call. // - An array of arguments to that function. - $operations[] = array( + $operations[] = [ 'batch_example_op_1', - array( + [ $i + 1, - t('(Operation @operation)', array('@operation' => $i)), - ), - ); + t('(Operation @operation)', ['@operation' => $i]), + ], + ]; } - $batch = array( - 'title' => t('Creating an array of @num operations', array('@num' => $num_operations)), + $batch = [ + 'title' => t('Creating an array of @num operations', ['@num' => $num_operations]), 'operations' => $operations, 'finished' => 'batch_example_finished', - ); + ]; return $batch; } @@ -118,15 +118,15 @@ class BatchExampleForm extends FormBase { public function generateBatch2() { $num_operations = 20; - $operations = array(); + $operations = []; // 20 operations, each one loads all nodes. for ($i = 0; $i < $num_operations; $i++) { - $operations[] = array( + $operations[] = [ 'batch_example_op_2', - array(t('(Operation @operation)', array('@operation' => $i))), - ); + [t('(Operation @operation)', ['@operation' => $i])], + ]; } - $batch = array( + $batch = [ 'operations' => $operations, 'finished' => 'batch_example_finished', // @current, @remaining, @total, @percentage, @estimate and @elapsed. @@ -141,7 +141,7 @@ class BatchExampleForm extends FormBase { 'init_message' => t('Batch 2 is starting.'), 'progress_message' => t('Processed @current out of @total.'), 'error_message' => t('Batch 2 has encountered an error.'), - ); + ]; return $batch; } diff --git a/batch_example/src/Tests/BatchExampleWebTest.php b/batch_example/tests/src/functional/BatchExampleWebTest.php similarity index 53% rename from batch_example/src/Tests/BatchExampleWebTest.php rename to batch_example/tests/src/functional/BatchExampleWebTest.php index 6a48028..bf83616 100644 --- a/batch_example/src/Tests/BatchExampleWebTest.php +++ b/batch_example/tests/src/functional/BatchExampleWebTest.php @@ -1,8 +1,8 @@ drupalCreateUser(array('access content')); + $web_user = $this->drupalCreateUser(['access content']); $this->drupalLogin($web_user); // Launch Batch 1. - $this->drupalPostForm('examples/batch_example', array('batch' => 'batch_1'), t('Go')); + $this->drupalPostForm('examples/batch_example', ['batch' => 'batch_1'], t('Go')); // Check that 1000 operations were performed. $this->assertText('1000 results processed'); // Launch Batch 2. - $this->drupalPostForm('examples/batch_example', array('batch' => 'batch_2'), t('Go')); + $this->drupalPostForm('examples/batch_example', ['batch' => 'batch_2'], t('Go')); // Check that 600 operations were performed. $this->assertText('600 results processed'); }