Problem/Motivation

This is a small issue of the documentation for the core batch system.

The sample function of batch-finished callback is defined with three parameters.

Sample callback_batch_finished():

function my_finished_callback($success, $results, $operations) {

https://api.drupal.org/api/drupal/core%21includes%21form.inc/group/batch...

But actually four parameters are passed to batch-finished callbacks as below.


function _batch_finished() {

  // ...

        $batch_set_result = call_user_func_array($batch_set['finished'], [$batch_set['success'], $batch_set['results'], $operations, \Drupal::service('date.formatter')->formatInterval($batch_set['elapsed'] / 1000)]);

  // ...

}

https://api.drupal.org/api/drupal/core%21includes%21batch.inc/function/_...

The fourth parameter which stores a string for elapsed time is passed to the callback function but not documented there.

Proposed resolution

Add the fourth parameter to the document.

In addition, I'd like to add the parameter to callback functions for tests in batch_test module.

Remaining tasks

  • Create patch
  • Review patch

User interface changes

(None)

API changes

(None)

Data model changes

(None)

Comments

hgoto created an issue. See original summary.

hgoto’s picture

Status: Active » Needs review
StatusFileSize
new4.65 KB

Here's a simple patch.

hgoto’s picture

john cook’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Thanks for the patch @hgoto.

There are a couple of points.

The calls to _batch_test_finished_helper() have been changed to add the $elapsed parameter, but the function specification hasn't been updated to receive it (in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc line 90).

Also callback_batch_finished() (in core/lib/Drupal/Core/Form/form.api.php) should be updated to include the $elapsed parameter and the code comment updated.

hgoto’s picture

Status: Needs work » Needs review

John, thanks for your kind review.

> but the function specification hasn't been updated to receive it (in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc line 90).

I didn't think it's important to change the behavior when I made the patch, but I agree with you. It's better to use the $elapsed in the function.

I adjusted the points you pointed. And we need to update some tests with this change and I update them, too.

hgoto’s picture

Oops. I didn't update any file in the previous comment.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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

Status: Needs review » Needs work
Issue tags: +Needs reroll

The patch in #6 look great. Both points from #4 have been addressed.

It just needs a re-roll and then I think it will be ready for RTBC.

kostyashupenko’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new8.87 KB

reroll + fix

john cook’s picture

Status: Needs review » Reviewed & tested by the community

The re-rolled patch applied cleanly and look good.

A drupal_set_message() was changed to \Drupal::messenger()->addMessage() but that was just changing deprecated code.

I'm happy with this so changing to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/includes/form.inc
    @@ -648,7 +648,8 @@ function template_preprocess_form_element_label(&$variables) {
    + *   // See _batch_finished() for more information about these paramaters.
    

    I've read the docs of _batch_finished() they don;t seem to be helpful. How about pointing to callback_batch_finished() as those are helpful.

  2. +++ b/core/lib/Drupal/Core/Form/form.api.php
    @@ -109,12 +109,15 @@ function callback_batch_operation($multiple_params, &$context) {
    + * @param $elapsed
    + *   A string which represents the elapsed time for the batch process.
      */
    

    This could do with an example - eg. 1 min 30 sec. Also this should be @param string $elapsed

  3. +++ b/core/modules/system/tests/src/Functional/Batch/ProcessingTest.php
    @@ -264,30 +264,31 @@ public function _resultStack($id, $value = 0) {
    +    $pattern_elapsed = ' \((\d+ min )?\d+ sec\)';
    

    code do with a comment explaining the looseness of the regex is to cope with variable amounts of elapsed time.

dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra
Issue tags: +ContributionWeekend2019

We will work in ContributionWeekend2019

dhirendra.mishra’s picture

Assigned: dhirendra.mishra » Unassigned
Status: Needs work » Needs review
StatusFileSize
new8.91 KB

Here is the patch.

john cook’s picture

Status: Needs review » Needs work
StatusFileSize
new1.89 KB

@dhirendra.mishra, thank you for the patch.

In future can you add an interdiff when you create a new patch - I helps with reviewing the patch. I've added an interdiff for 9 to 14.

Following on from @alexpott's comments in #12, there are still a few changes needed.

  1. +++ b/core/includes/form.inc
    @@ -648,7 +648,8 @@ function template_preprocess_form_element_label(&$variables) {
    + *   // See callback_batch_finished() for more information about these paramaters.
    

    There is a spelling mistake in "parameters".

    The comment now goes beyond 80 characters - although it wouldn't if the code was pasted as a function. I'm not not sure of the standards for this maybe @alexpott can advise?

  2. +++ b/core/lib/Drupal/Core/Form/form.api.php
    @@ -109,12 +109,15 @@ function callback_batch_operation($multiple_params, &$context) {
    + * @param string $elapsed
    + *   A string which represents the elapsed time for the batch process.
    

    Examples are still missing as requested by @alexpott in #12 point 2.

  3. +++ b/core/modules/system/tests/src/Functional/Batch/ProcessingTest.php
    @@ -264,30 +264,32 @@ public function _resultStack($id, $value = 0) {
    +    // eg. 1 min 30 sec
    +    $pattern_elapsed = ' \((\d+ min )?\d+ sec\)';
    

    The comment should be an explanation of what the regex does. Something like The elapsed time should be either in minutes and seconds or seconds alone.

    Could the elapsed time be in multiple minutes (like "2 mins 10 secs")? Do the parts get pluralised (adding 's')? Would the regex match in these cases?

dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra

i will work on your comment. Thanks @John Cook

dhirendra.mishra’s picture

Assigned: dhirendra.mishra » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.73 KB
new9 KB

Here is the interdiff and updated patch as per #15 comment.

john cook’s picture

Status: Needs review » Needs work

Thanks for the new patch and interdiff, @ dhirendra.mishra

All the points from #15 have been addressed.

But applying the patch gives the following error:

../drupal-add_fourth_parameter_to_batch_finished_callback_sample-2939645-17.patch:11: trailing whitespace.
 *   // See callback_batch_finished() for more information about these 
warning: 1 line adds whitespace errors.

This seems to be a coding standards error.

The next step is to check the coding standards error. These can be viewed on the test results page (click the green/gray/red element near the patch file) and there will be a coding standards messages link.

govind.maloo’s picture

Status: Needs work » Needs review
StatusFileSize
new8.98 KB

Fixed trailing space issue.

john cook’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for the patch @govind.maloo.

The only change between patches #17 and #19 is the removal of white space. and patch #19 applies cleanly.

As the issues pointed out in previous comments are also fixed, I'm setting this issue to RTBC.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs review

This review point from @alexpott was not covered AFAIS, however it looks important to avoid random fails.

Could the elapsed time be in multiple minutes (like "2 mins 10 secs")? Do the parts get pluralised (adding 's')? Would the regex match in these cases?

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

StatusFileSize
new5.02 KB
new10.68 KB

Reroll and modify the regex pattern mentioned in #21.

quietone’s picture

StatusFileSize
new731 bytes
new10.68 KB
+++ b/core/lib/Drupal/Core/Form/form.api.php
@@ -109,13 +109,17 @@ function callback_batch_operation($multiple_params, &$context) {
+ *   A string which represents the elapsed time for the batch process, eg.
+ *   e.g. 1 min 30 sec.

There are two versions of e.g. instead of one.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

jungle’s picture

  1. +++ b/core/includes/form.inc
    @@ -650,7 +650,9 @@ function template_preprocess_form_element_label(&$variables) {
    + *   // See callback_batch_finished() for more information about these
    

    #12.1 addressed

  2. +++ b/core/lib/Drupal/Core/Form/form.api.php
    @@ -109,13 +109,17 @@ function callback_batch_operation($multiple_params, &$context) {
    + *   A string representing the elapsed time for the batch process, e.g., 1 min
    + *   30 sec.
    

    #12.2 addressed. BUT, I think it should be 30 secs.

    Suggestion:

    - *   A string representing the elapsed time for the batch process, e.g., 1 min
    - *   30 sec.
    + *   A string representing the elapsed time for the batch process, e.g.,
    + *   '1 min 30 secs'.
    

    Do not separate 1 min and 30 sec(s) due to over 80 chars. Instead, treat them together as ONE, surrounding with single quotes, stays at the 2nd line. Neither over chars, nor wrapped early.

  3. +++ b/core/modules/system/tests/src/Functional/Batch/ProcessingTest.php
    @@ -313,38 +313,40 @@ public function _resultStack($id, $value = 0) {
    +    // The elapsed time should be either in minutes and seconds or only seconds.
    +    $pattern_elapsed = ' \((\d+ mins? )?\d+ secs?\)';
    

    Addressed #12.3, and the reg looks good.

  4. Tagging "Bug Smash Initiative"

In all, this is RTBC to me.

alexpott’s picture

Version: 9.1.x-dev » 9.0.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 7ca98bac22 to 9.1.x and 28c8fbe32e to 9.0.x. Thanks!

  • alexpott committed 7ca98ba on 9.1.x
    Issue #2939645 by quietone, hgoto, dhirendra.mishra, jungle,...

  • alexpott committed 28c8fbe on 9.0.x
    Issue #2939645 by quietone, hgoto, dhirendra.mishra, jungle,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.