Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

How to use Batch API to do heavy processing in Drupal 8 & 9

 

The batch API in Drupal allows heavy and / or long processing to be carried out, without the risk of having an execution time exceeded on the part of its server.

in this article i want to do an example how to use the batch api.

So I have a route defined which calls the static "batchExample" method of my controller.

Function of who prepares the batch to be processed:

public function batchExample() {
    
 $batch = array(
            'title' => t('Import Data...'),
            'operations' => [],
            'init_message' => t('Commencing'),
            'progress_message' => t('Processed @current out of @total.'),
            'error_message' => t('An error occurred during processing'),
            'finished' => 'import_data_finished',
        );
        for ($i = 0; $i < 100; $i++) {
            $batch['operations'][] = ['import_data_test', [$i]];
        }

        batch_set($batch);

        // Only needed if not inside a form _submit handler.
        // Setting redirect in batch_process.
       return batch_process('/admin/content');

  }

 

here is an example if you want to use batch api inside form submit handler:

public function batchExample() { 
 $batch = array(
            'title' => t('Import Data...'),
            'operations' => [],
            'init_message' => t('Commencing'),
            'progress_message' => t('Processed @current out of @total.'),
            'error_message' => t('An error occurred during processing'),
            'finished' => 'import_data_finished',
        );
        for ($i = 0; $i < 100; $i++) {
            $batch['operations'][] = ['import_data_test', [$i]];
        }
        batch_set($batch);
  }

 

Here is the method called at the operations:

function import_data_test($nbre = 0, &$context)
{
    $context['message'] = 'Import Data N° ' . $nbre;
    $context['results'][] = $nbre;
    usleep(50000);
}

 

Here is the method called at the end of the batch:

function import_data_finished($success, $results, $operations)
{
    if ($success) {
        $message = \Drupal::translation()->formatPlural(
            count($results),
            'One item processed.', '@count items processed.'
        );
    } else {
        $message = t('Finished with an error.');
    }
    \Drupal::messenger()->addStatus($message);
}

 

screenshot for our example:

How to use Batch API to do heavy processing in Drupal 8 & 9

 

Nothing too complicated then, just add the import_data_test() and import_data_finished() methods in MODULENAME.module.

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon