在Drupal8下面,第一次写批处理,写好以后,运行,报了这样的错误信息:
Error:Using $this when not in object context 在函数 Drupal\qipu\Form\QipuConvertForm::processConvert()(文件 /../modules/qipu/src/Form/QipuConvertForm.php 第 87 行)#0 /../core/includes/batch.inc(252): Drupal\qipu\Form\QipuConvertForm::processConvert(Array) #1 /../core/includes/batch.inc(95): _batch_process() #2 /../core/includes/batch.inc(77): _batch_do() #3 /../core/modules/system/src/Controller/BatchController.php(55): _batch_page(Object(Symfony\Component\HttpFoundation\Request)) #4 [internal function]: Drupal\system\Controller\BatchController->batchPage(Object(Symfony\Component\HttpFoundation\Request)) #5
Deprecated function:Non-static method Drupal\qipu\Form\QipuConvertForm::processConvert() should not be called statically 在函数 _batch_process()(文件 /../core/includes/batch.inc 第 252 行)#0 /../core/includes/bootstrap.inc(552): _drupal_error_handler_real(8192, 'Non-static meth...',
通过搜索,发现,原来的代码写的有问题:
$batch = array(
'operations' => array(
array(array('\Drupal\qipu\Form\QipuConvertForm', 'processConvert'), array())
),
'finished' => array('\Drupal\qipu\Form\QipuConvertForm', 'finishConvert'),
'title' => t('转换中'),
'error_message' => t('转换中遇到错误.'),
);
batch_set($batch);
批处理是在\Drupal\qipu\Form\QipuConvertForm这个类中调用的,这里正确的写法是:
$batch = array(
'operations' => array(
array(array($this, 'processConvert'), array())
),
//'\Drupal\qipu\Form\QipuConvertForm'
'finished' => array($this, 'finishConvert'),
'title' => t('转换中'),
'error_message' => t('转换中遇到错误.'),
);
batch_set($batch);
需要将当前类名的全称,改为 $this即可。