Why doesn't the batch process start? I've created a class that extend the 'FormBase' class to create a form where I can upload a XLSX file to import into custom DB table.

To process the import I would use the 'Drupal batch process' to show to the user the progress of import.

These are the methods I wrote.

    class CommissionerImportUmpiresListForm extends FormBase {

      public function submitForm(array &amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) {
        $form_file = $form_state-&gt;getValue('my_file', 0);
        if (isset($form_file[0]) &amp;&amp; !empty($form_file[0])) {
          $f = File::load($form_file[0]);
          $f-&gt;setPermanent();
          $f-&gt;save();
          $this-&gt;setSheetData($this-&gt;commissioner_read_file($f-&gt;getFileURI()));
          $orig = $this-&gt;getSheetData();
          $clean = array_shift($orig);
          $this-&gt;setSheetData($orig);
          $operations = [];
          $c = count($this-&gt;getSheetData());
          foreach ($this-&gt;getSheetData() as $row) {
            $operations[] = [
              ['Drupal\commissioner\Form\CommissionerImportUmpiresListForm', 'batch_run'],
              [$row, $c],
            ];
          }

          $batch = [
            'title' =&gt; t('Importing the umpires list...'),
            'operations' =&gt; $operations,
            'finished' =&gt; ['Drupal\commissioner\Form\CommissionerImportUmpiresListForm', 'batch_finished'],
            'init_message' =&gt; t('Importing is starting.'),
            'progress_message' =&gt; t('Processed @current out of @total.'),
            'error_message' =&gt; t('Importing has encountered an error.'),
          ];
          batch_set($batch);
          batch_process('admin/config/content/umpire_list');
        }
      }

      private function commissioner_read_file($file) {
        $sheetData = [];

        try {
          $real_path = \Drupal::service('file_system')-&gt;realpath($file);
          $inputFileType = IOFactory::identify($real_path);
          $reader = IOFactory::createReader($inputFileType);
          $spreadsheet = $reader-&gt;load($real_path);
          $sheetData = $spreadsheet-&gt;getActiveSheet()-&gt;toArray(NULL, TRUE, TRUE, TRUE);
        }
        catch (Exception $catch) {
          \Drupal::messenger()-&gt;addMessage($catch-&gt;getMessage(), \Drupal\Core\Messenger\MessengerInterface::TYPE_ERROR);
        }

        return $sheetData;
      }

      public static function batch_run($row, $count, &amp;$context) {
        if (empty($context['sandbox'])) {
          $context['sandbox']['progress'] = 0;
          $context['sandbox']['max'] = $count;
        }

        $context['sandbox']['progress']++;
        $context['sandbox']['current_umpire'] = $row;

        // Do something with the row.
        $result = \Drupal ::database()
          -&gt;insert('mytable')
          -&gt;fields([
            'cognome' =&gt; $row['A'],
            'nome' =&gt; $row['B'],
            'categoria' =&gt; $row['Z'],
            'qualifica' =&gt; $row['AA'],
            'livello' =&gt; $row['AB'],
            'comitato' =&gt; $row['AC'],
            'email' =&gt; $row['T'],
            'cell' =&gt; $row['S'],
          ])-&gt;execute();

        $context['results'][] = $row;
      }

      public static function batch_finished($success, $results, $operations, $elapsed) {
        if ($success) {
          $message = \Drupal::translation()
            -&gt;formatPlural(count($results), 'One umpire imported.', '@count umpires imported.');
        }
        else {
          $message = t('Finished with an error.');
        }

        \Drupal::messenger()-&gt;addMessage($message, \Drupal\Core\Messenger\MessengerInterface::TYPE_STATUS);
      }
    }


I tried executing the form, but after the initial XLSX reader, that created the array with the rows in the file, the batch process doesn't start.

What's wrong? Why doesn't the batch process start?
Check out the example on Batch operations. And remove batch_process() from your code. Since you are starting the batch in a form submit handler batch_process() is interrupting that. Just batch_set() and Form API takes care of the rest.


// Only needed if not inside a form _submit handler.
// Setting redirect in batch_process.
batch_process('node/1');