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).

Comments

balazswmann’s picture

StatusFileSize
new761 bytes

I 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'];

balazswmann’s picture

I have attached the Drupal 7 version of the patch attached in #1.

balazswmann’s picture

Status: Active » Needs review
balazswmann’s picture

Issue summary: View changes
alansaviolobo’s picture

StatusFileSize
new837 bytes

reroll

Status: Needs review » Needs work

The last submitted patch, 5: make_batch_set_more-2208287-5.patch, failed testing.

alansaviolobo’s picture

StatusFileSize
new761 bytes
alansaviolobo’s picture

Status: Needs work » Needs review

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

john cook’s picture

I've done another re-roll of #7.

john cook’s picture

StatusFileSize
new714 bytes
john cook’s picture

Status: Needs review » Needs work

As 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.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.