View source
<?php
namespace Drupal\views_bulk_operations;
use Drupal\Core\Url;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionCompletedTrait;
class ViewsBulkOperationsBatch {
use ViewsBulkOperationsActionCompletedTrait;
public static function getList(array $data, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['processed'] = 0;
$context['sandbox']['page'] = 0;
$context['sandbox']['total'] = $data['exclude_mode'] ? $data['total_results'] - count($data['exclude_list']) : $data['total_results'];
$context['sandbox']['npages'] = ceil($data['total_results'] / $data['batch_size']);
$context['results'] = $data;
}
$actionProcessor = \Drupal::service('views_bulk_operations.processor');
$actionProcessor
->initialize($data);
$list = $actionProcessor
->getPageList($context['sandbox']['page']);
$count = count($list);
foreach ($list as $item) {
$context['results']['list'][] = $item;
}
$context['sandbox']['page']++;
$context['sandbox']['processed'] += $count;
if ($context['sandbox']['page'] <= $context['sandbox']['npages']) {
$context['finished'] = 0;
$context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];
$context['message'] = static::translate('Prepared @count of @total entities for processing.', [
'@count' => $context['sandbox']['processed'],
'@total' => $context['sandbox']['total'],
]);
}
}
public static function saveList($success, array $results, array $operations) {
if ($success) {
$results['redirect_url'] = $results['redirect_after_processing'];
unset($results['redirect_after_processing']);
$tempstore_factory = \Drupal::service('tempstore.private');
$current_user = \Drupal::service('current_user');
$tempstore_name = 'views_bulk_operations_' . $results['view_id'] . '_' . $results['display_id'];
$results['prepopulated'] = TRUE;
$tempstore_factory
->get($tempstore_name)
->set($current_user
->id(), $results);
}
}
public static function operation(array $data, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['processed'] = 0;
$context['results']['operations'] = [];
$context['sandbox']['page'] = 0;
$context['sandbox']['npages'] = ceil($data['total_results'] / $data['batch_size']);
}
$actionProcessor = \Drupal::service('views_bulk_operations.processor');
$actionProcessor
->initialize($data);
$count = $actionProcessor
->populateQueue($data, $context);
$batch_results = $actionProcessor
->process();
if (!empty($batch_results)) {
foreach ($batch_results as $result) {
$context['results']['operations'][] = (string) $result;
}
}
$context['sandbox']['processed'] += $count;
$context['sandbox']['page']++;
if ($context['sandbox']['page'] <= $context['sandbox']['npages']) {
$context['finished'] = 0;
$context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];
$context['message'] = static::translate('Processed @count of @total entities.', [
'@count' => $context['sandbox']['processed'],
'@total' => $context['sandbox']['total'],
]);
}
}
public static function getBatch(array &$view_data) {
$current_class = get_called_class();
if (empty($view_data['list'])) {
$view_data['redirect_after_processing'] = $view_data['redirect_url'];
$view_data['redirect_url'] = Url::fromRoute('views_bulk_operations.execute_batch', [
'view_id' => $view_data['view_id'],
'display_id' => $view_data['display_id'],
]);
$batch = [
'title' => static::translate('Prepopulating entity list for processing.'),
'operations' => [
[
[
$current_class,
'getList',
],
[
$view_data,
],
],
],
'progress_message' => static::translate('Prepopulating, estimated time left: @estimate, elapsed: @elapsed.'),
'finished' => [
$current_class,
'saveList',
],
];
}
else {
$batch = [
'title' => static::translate('Performing @operation on selected entities.', [
'@operation' => $view_data['action_label'],
]),
'operations' => [
[
[
$current_class,
'operation',
],
[
$view_data,
],
],
],
'progress_message' => static::translate('Processing, estimated time left: @estimate, elapsed: @elapsed.'),
'finished' => $view_data['finished_callback'],
];
}
return $batch;
}
}