How to pass parameters by reference to a batch?
-
21-02-2021 - |
سؤال
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;
}
لا تنتمي إلى drupal.stackexchange