In Drupal 7 from the function called with 'url/to/call/progressbar/update/function' (see below) you would return an array converted to JSON like this:


I cannot find any examples for Drupal 8 and I suspect it's an object rather than an array that gets returned.

Does anyone know how/what to return from the progress bar update function?

The setup for the bar progress indicator would be something like this:

$form['some_item'] = array(
  '#type'  => 'submit',
  '#value' => t('My action'),
  '#ajax'  => array(
    'callback' => 'a_function',
    'wrapper'  => 'ajax-response-goes-here',
    'method'   => 'replace',
    'progress' => array (
      'type'     => 'bar',
      'message'  => 'Starting...',
      'url'      => 'url/to/call/progressbar/update/function',
      'interval' => '1000'

The core example is in FileWidgetAjaxController::progress. You need to return a JSON-encoded associative array with message and percentage keys. For example:

$progress['message'] = t('Uploading... (@current of @total)', ['@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])]);
$progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
return new JsonResponse($progress);

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.