O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating the batch

Inside the JsonImporter::import() method, once we get our hands on the $products array, let's replace the loop with the following:

$batch = [
  'title' => $this->t('Importing products'),
  'operations' => [
    [[$this, 'clearMissing'], [$products]],
    [[$this, 'importProducts'], [$products]],
  ],
  'finished' => [$this, 'importProductsFinished'],
];
batch_set($batch);

Creating a batch involves a number of steps, the first one being the creation of a batch definition, which is nothing more than an array with some data. The title option sets the title to be used on the progress page. Similarly, we also have the optional init_message, progress_message and error_message, which have some sensible defaults, so we won't set them here. For ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required