質問

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.
        ...
    }
}
役に立ちましたか?

解決

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) {
     ...
   }
}
ライセンス: CC-BY-SA帰属
所属していません drupal.stackexchange
scroll top