You are here

public function BatchExampleForm::generateBatch2 in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/batch_example/src/Form/BatchExampleForm.php \Drupal\batch_example\Form\BatchExampleForm::generateBatch2()

Generate Batch 2.

Batch 2 will process five items at a time.

This creates an operations array defining what batch 2 should do, including what it should do when it's finished. In this case, each operation is the same and by chance even has the same $nid to operate on, but we could have a mix of different types of operations in the operations array.

1 call to BatchExampleForm::generateBatch2()
BatchExampleForm::submitForm in batch_example/src/Form/BatchExampleForm.php
Form submission handler.

File

batch_example/src/Form/BatchExampleForm.php, line 118

Class

BatchExampleForm
Form with examples on how to use cache.

Namespace

Drupal\batch_example\Form

Code

public function generateBatch2() {
  $num_operations = 20;
  $operations = [];

  // 20 operations, each one loads all nodes.
  for ($i = 0; $i < $num_operations; $i++) {
    $operations[] = [
      'batch_example_op_2',
      [
        $this
          ->t('(Operation @operation)', [
          '@operation' => $i,
        ]),
      ],
    ];
  }
  $batch = [
    'operations' => $operations,
    'finished' => 'batch_example_finished',
    // @current, @remaining, @total, @percentage, @estimate and @elapsed.
    // These placeholders are replaced with actual values in _batch_process(),
    // using strtr() instead of t(). The values are determined based on the
    // number of operations in the 'operations' array (above), NOT by the
    // number of nodes that will be processed. In this example, there are 20
    // operations, so @total will always be 20, even though there are multiple
    // nodes per operation.
    // Defaults to t('Completed @current of @total.').
    'title' => $this
      ->t('Processing batch 2'),
    'init_message' => $this
      ->t('Batch 2 is starting.'),
    'progress_message' => $this
      ->t('Processed @current out of @total.'),
    'error_message' => $this
      ->t('Batch 2 has encountered an error.'),
  ];
  return $batch;
}