At the moment, batch_set() ignores any kind of initial data will be available in $context['results'] during the batch process, however this would be very useful in some cases.
Lets say I have a pretty large XML document which describes some kind of book. So I would like to import the data from this XML document using batch API, and at the end of the import process I would like to set a message with some basic information about the freshly imported book, including for example it's title and it's author. In other words, the title and the author attributes need to be available at the end of the batch process. More specifically, they need to be available in $context['results'], because this array will be passed to the batch finish callback.
To achieve the behavior described above, it would be very nice if I would place the book's title and it's author in the batch definition array somewhere next to other batch attributes before the the batch process starts, so they will be available in all batch operations (including the first one) and also in the "finished" callback. The simplest solution is to modify batch_set() to accept initial "results" attribute from the batch definition like this:
$batch = array(
'title' => t('Importing book'),
'operations' => $operations, // $operations defined earlier
'init_message' => t('Initializing: book import'),
'error_message' => t('An error occurred during book import.'),
'finished' => 'book_import_finished_callback',
'results' => array(
'book' => (object) array(
'title' => $book_title, // $book_title defined earlier
'author' => $book_author, // $book_author defined earlier
),
),
);
batch_set($batch);
One advantage of the above solution is that it is require only 2 line changes in batch API. Another solution would be a new optional 'init_parameters' attribute (or something like this) in the batch definition, whose value would be copied into $context['results'] before the start of the batch process, or would be available next to it for example in $context['init_params'] or similar ways.
Without these features one workaround can be if I pass book's title and book's author to each batch operation individually as a parameter, but this is a very ugly solution, because batch operation parameters should contain information only for the actual operation and not "shared" information available or used in all operations. Another workaround if I alter my own batch with hook_batch_alter() and inside of it I can pass "initial data" into $context['results'], because in here it is already available, and it won't be altered anymore before the start of the batch process. I guess, I don't have to say that this latter workaround even worse than the first one.
If any changes will be made, don't forget to update batch API's documentation according to the new feature(s).
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | make_batch_set_more-2208287-15.patch | 714 bytes | john cook |
| #7 | make_batch_set_more-2208287-7.patch | 761 bytes | alansaviolobo |
| #5 | make_batch_set_more-2208287-5.patch | 837 bytes | alansaviolobo |
| #2 | D7-batch_set-initial-data-2208287-2-do-not-test.patch | 708 bytes | balazswmann |
| #1 | batch_set-initial-data-2208287-1.patch | 761 bytes | balazswmann |
Comments
Comment #1
balazswmann commentedI have attached a patch which makes the initial 'results' attribute available in batch definition array, and it's value will be available during the batch process under $context['results'];
Comment #2
balazswmann commentedI have attached the Drupal 7 version of the patch attached in #1.
Comment #3
balazswmann commentedComment #4
balazswmann commentedComment #5
alansaviolobo commentedreroll
Comment #7
alansaviolobo commentedComment #8
alansaviolobo commentedComment #15
john cook commentedI've done another re-roll of #7.
Comment #16
john cook commentedComment #17
john cook commentedAs the batch builder was added in #2401797: Introduce a batch builder class to make the batch API easier to use, the initial results need to be set using that as well.