This might not happen a lot but when I was doing a batch process with issues, I received the error Notice: Undefined variable: label in _batch_process().

There is a case that the $label variable will not get set.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

albertski created an issue. See original summary.

albertski’s picture

Status: Active » Needs review
FileSize
507 bytes

The following patch fixes the issue by setting $label to blank by default.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

A bit confused about the difference between $task_message and $label in this function, as they appear to basically be the same thing? It feels like this whole function needs a refactor as it is quite confusing to follow.

But, this is a simple fix, so RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

As we're not adding a test I'd rather do something that eliminates the $label. It is essential useless. We could initialise $task_message outside the first while loop and re-initialise it in the loop too and that way we can remove label and have no conditionality to test.

alexpott’s picture

Also an update to the _batch_process return value would be great so we know what this is and that it should be there.

albertski’s picture

Status: Needs work » Needs review
FileSize
1.28 KB

Good point @alexpott.

alexpott’s picture

Status: Needs review » Needs work
+++ b/core/includes/batch.inc
@@ -276,7 +277,6 @@ function _batch_process() {
-    $task_message = $label = '';

I'm not sure that this removal is correct - I think the purposed is to reinitialize the variable for each loop. I'd leave $task_message = ''; otherwise we'll be changing the inputs to

      // Build the 'context' array and execute the function call.
      $batch_context = [
        'sandbox'  => &$current_set['sandbox'],
        'results'  => &$current_set['results'],
        'finished' => &$finished,
        'message'  => &$task_message,
      ];
      call_user_func_array($callback, array_merge($args, [&$batch_context]));
albertski’s picture

Status: Needs work » Needs review
FileSize
979 bytes

Made the change.

longwave’s picture

Status: Needs review » Needs work

We still need to initialise it outside the loop, in case the loop is never entered.

dhirendra.mishra’s picture

Thanks for update. Updated the correct patch with variable initialized out of loop.

albertski’s picture

Status: Needs work » Needs review
FileSize
1.3 KB

Updated the patch to initialize outside the loop and inside the loop.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

#11 looks good, thanks.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed f159bbd and pushed to 8.7.x. Thanks!

  • catch committed f159bbd on 8.7.x
    Issue #2999473 by albertski, dhirendra.mishra, longwave, alexpott:...

Status: Fixed » Closed (fixed)

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