View source
<?php
namespace Drupal\views_bulk_operations;
use Drupal\Core\Url;
class ViewsBulkOperationsBatch {
public static function t($string, array $args = [], array $options = []) {
return \Drupal::translation()
->translate($string, $args, $options);
}
public static function message($message = NULL, $type = 'status', $repeat = TRUE) {
drupal_set_message($message, $type, $repeat);
}
public static function getList(array $data, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['processed'] = 0;
$context['results'] = $data;
}
$actionProcessor = \Drupal::service('views_bulk_operations.processor');
$actionProcessor
->initialize($data);
$count = $actionProcessor
->populateQueue([], $context);
if ($count) {
foreach ($actionProcessor
->getQueue() as $index => $entity) {
$item = [
$index,
$entity
->language()
->getId(),
$entity
->getEntityTypeId(),
$entity
->id(),
];
$context['results']['list'][] = $item;
}
$context['sandbox']['processed'] += $count;
$context['finished'] = 0;
if ($context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];
$context['message'] = static::t('Prepared @count of @total entities for processing.', [
'@count' => $context['sandbox']['processed'],
'@total' => $context['sandbox']['total'],
]);
}
else {
$context['message'] = static::t('Prepared @count entities for processing.', [
'@count' => $context['sandbox']['processed'],
]);
}
}
}
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('user.private_tempstore');
$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'] = [];
}
$actionProcessor = \Drupal::service('views_bulk_operations.processor');
$actionProcessor
->initialize($data);
$count = $actionProcessor
->populateQueue($data['list'], $context);
if ($count) {
$batch_results = $actionProcessor
->process();
if (!empty($batch_results)) {
foreach ($batch_results as $result) {
$context['results']['operations'][] = (string) $result;
}
}
$context['sandbox']['processed'] += $count;
$context['finished'] = 0;
if ($context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];
$context['message'] = static::t('Processed @count of @total entities.', [
'@count' => $context['sandbox']['processed'],
'@total' => $context['sandbox']['total'],
]);
}
else {
$context['message'] = static::t('Processed @count entities.', [
'@count' => $context['sandbox']['processed'],
]);
}
}
}
public static function finished($success, array $results, array $operations) {
if ($success) {
$operations = array_count_values($results['operations']);
$details = [];
foreach ($operations as $op => $count) {
$details[] = $op . ' (' . $count . ')';
}
$message = static::t('Action processing results: @operations.', [
'@operations' => implode(', ', $details),
]);
static::message($message);
}
else {
$message = static::t('Finished with an error.');
static::message($message, 'error');
}
}
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::t('Prepopulating entity list for processing.'),
'operations' => [
[
[
$current_class,
'getList',
],
[
$view_data,
],
],
],
'progress_message' => static::t('Prepopulating, estimated time left: @estimate, elapsed: @elapsed.'),
'finished' => [
$current_class,
'saveList',
],
];
}
else {
$batch = [
'title' => static::t('Performing @operation on selected entities.', [
'@operation' => $view_data['action_label'],
]),
'operations' => [
[
[
$current_class,
'operation',
],
[
$view_data,
],
],
],
'progress_message' => static::t('Processing, estimated time left: @estimate, elapsed: @elapsed.'),
'finished' => [
$current_class,
'finished',
],
];
}
return $batch;
}
}