-2

I checked the documentation about the callbacks used from the batch API, but I didn't find any information about the finished callback being mandatory or not.

Is that callback mandatory or optional?

  • Just try it out yourself. Does it work? Also look at the docs and core code of batch implementations. Maybe it's not mandatory. Maybe it's best practice anyways. – leymannx Aug 7 '18 at 8:35
1

Finished callback is a optional function to show a message error, or even save the result.
Drupal core verifies if there is the finished callback, so there aren't problems if that callback is not defined.

/**
 * Ends the batch processing.
 *
 * Call the 'finished' callback of each batch set to allow custom handling of
 * the results and resolve page redirection.
 */
function _batch_finished() {
  $batch = &batch_get();

  // Execute the 'finished' callbacks for each batch set, if defined.
  foreach ($batch['sets'] as $batch_set) {
    if (isset($batch_set['finished'])) {
      // Check if the set requires an additional file for function definitions.
      if (isset($batch_set['file']) && is_file($batch_set['file'])) {
        include_once DRUPAL_ROOT . '/' . $batch_set['file'];
      }
      if (is_callable($batch_set['finished'])) {
        $queue = _batch_queue($batch_set);
        $operations = $queue->getAllItems();
        call_user_func($batch_set['finished'], $batch_set['success'], $batch_set['results'], $operations, format_interval($batch_set['elapsed'] / 1000));
      }
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.