0

I could setup a nice batch, everything is working well.

But when the batch is finished, I would like to display a beautiful table with the computed results.

I could do it with the drupal_set_message (see below) function, but it is not good enough. [if mandatory, I accept to be redirected to a new page... but how can I pass the result in this case?]

function my_batch_finished_callback($success, $results, $operations) {
    if ($success) {
        $header=[t("col1"),t("col2")];
        drupal_set_message( theme('table', ['header' => $header, 'rows' => $results]));
    }
    else {
        // An error occurred.
        ...
    }
}
6
  • You haven't provided enough information for a full solution but essentially you'll have to store the results somewhere for retrieval on the new page. Simplest would be a DB table which includes an index by a key identifying the batch (eg the batch ID itself). Dec 6, 2019 at 10:01
  • @ALfred Amstrong, many thanks for your help: if I understand well, there is no available "hook/api" to display the result of a batch. As my $results array is not to big, maybe I could use variable_set with the id of the batch and redirect to a page like /mybatch/results/{batch_id} and use variable_get to display the table
    – Baud
    Dec 6, 2019 at 11:54
  • if you are looking for a quick imperfect solution, I'd suggest you save it in cache rather than the variable table which is not good to let grow indefinitely. Dec 6, 2019 at 13:25
  • my idea was to delete the variable just after the page is displayed... Also, note that I couldn't get the id of the batch from within a submit function... this is why I used a timestamp as id. Just curious: how can I save to the cache?
    – Baud
    Dec 6, 2019 at 13:37

1 Answer 1

2

You need to store your results somewhere to be able to display them. If they are only required temporarily, the Drupal cache may be an appropriate place to use.

You'll need a key to identify the cache item and make it unique to the batch. As batch callbacks are not passed the internal batch ID, which would be the ideal choice, you'll need to generate one, for which you can use the uniqid() function.

So your batch finished callback will be something like:

function mymodule_batch_finished($success, $results, $operations) {
   if ($success) {
     $key = uniqid();
     cache_set('mymodule_batch_results:' . $key, $results, 'cache',time() + 86400); // keep for 1 day 
     drupal_goto('mymodule/batch-results/' + $key);
   }
   ...
}

In the callback function for the redirected page you can extract the key from the path and read the data from the cache.

function mymodule_menu() {
  $items['mymodule/batch-results/%'] = [
     ...
     'page callback' => 'mymodule_display_results',
     'page arguments => [2,],
     ...
  ];
}

function mymodule_display_results($key) {
   $data = cache_get('mymodule_batch_results:' . $key);
   if ($data) {
     ...
   }
}
2
  • Very nice! Thank you.
    – Baud
    Dec 6, 2019 at 14:30
  • I have corrected the cache_set call (missing arguments)
    – Baud
    Dec 6, 2019 at 14:38

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.