質問

I need to process a parameter in a batch function where the result will be kept after the function ends and it should be then passed in the finished function of the same batch. Is there a way to pass the parameters by a reference, so that any changes to that parameter are saved?

Example:

$params = [$a, $b];
$finishedParams = [$a, $b];
$batch = ['title' => 'Sample processing batch',
          'operations' => ['someFunction', $params],
          'finished' => ['finishedFunction', $finishedParams]
         ];
...
public static function someFunction(&$a, $b){
    $a = 'somethingChanged';
}
public static function finishedFunction($success, $results, $operations){
   // Use $a variable when somethingChanged after the batch process.
}
役に立ちましたか?

解決

Alfred is right. You can achieve that by using the $context parameter.

public static function someFunction($a, $b, &$context) {
  // Store data in $context['results'] entry.
  // my_var will keep that value.
  $context['results']['my_var'] = 20;
}
ライセンス: CC-BY-SA帰属
所属していません drupal.stackexchange
scroll top