5

This is the code I am using.

$serviceClass = \Drupal::service('abcclass.service');
$batch = array(
  'title' => t('Pushing updated.'),
  'init_message' => t('Start Pushing.'),
  'progress_message' => t('Completed @current step of @total.'),
  'error_message' => t('Pushing has encountered an error.'),
  'operations' => array(
    array(array($serviceClass , 'ebook_push_single_pdf'), $node->id()),
  ),
  'finished' => array($serviceClass , 'ebook_push_finished'),
);
batch_set($batch);

But it doesn't work. I checked log file and I get:

Settings can not be serialized. This probably means you are serializing an object that has an indirect reference to the Settings object. Adjust your code so that is not necessary.

Note that if I implement batch callbacks in same file, it works.

How can I fix the code?

5

When I use a callback function what live in a class I use, for example:

'finished' => array('\Drupal\my_batch\Form\MyBatchForm', 'finish_function'),

The same with 'operations':

'operations' => array(array('\Drupal\my_batch\Form\MyBatchForm', 'operation_function'), array($node->id())),
  • Your example of operations makes no sense. There are more closing brackets than opening brackets and it looks like the whole thing should be wrapped in another array to allow for multiple operations. – Felix Eve Aug 11 '16 at 5:01
  • @FelixEve thank you, fixed the closing brackets xD – rpayanm Dec 26 '16 at 15:03
1

A $serviceClass cannot be passed through the form to a batch file like this. It has to be passed as an array of arguments inside the operations.

Fixed Code is here -

$serviceClass = \Drupal::service('abcclass.service');
$batch = array(
  'title' => $this->t('Pushing updated.'),
  'init_message' => $this->t('Start Pushing.'),
  'progress_message' => $this->t('Completed @current step of @total.'),
  'error_message' => $this->t('Pushing has encountered an error.'),
  'operations' => array(
    array(
      array('Drupal\module_name\Batch_file','run'),
      array($serviceClass , 'ebook_push_single_pdf', $node->id(),'import'),
    ),
  ),
  'finished' => array('Drupal\module_name\Batch_file','finished'),
);
batch_set($batch);

where, "Batch_file" is the Batch_file.php where we get those arguments.

'run' and 'finished' are the two static functions of Batch_file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.