Commit d38f09d8 authored by Marcin Grabias's avatar Marcin Grabias
Browse files

WIP to be rebased.

parent 21c6d1d2
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -2,11 +2,12 @@

namespace Drupal\views_bulk_operations_example\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface;

/**
 * An example action covering most of the possible options.
@@ -42,7 +43,7 @@ class ViewsBulkOperationExampleAction extends ViewsBulkOperationsActionBase impl
    // ...
    $this->messenger()->addMessage($entity->label() . ' - ' . $entity->language()->getId() . ' - ' . $entity->id());
    return $this->t('Example action (configuration: @configuration)', [
      '@configuration' => print_r($this->configuration, TRUE)
      '@configuration' => Markup::create(print_r($this->configuration, TRUE))
    ]);
  }

+19 −8
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\views_bulk_operations\Action;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Component\Render\FormattableMarkup;

/**
 * Defines action completion logic.
@@ -39,15 +40,25 @@ trait ViewsBulkOperationsActionCompletedTrait {
   */
  public static function finished($success, array $results, array $operations): ?RedirectResponse {
    if ($success) {
      $operations = array_count_values($results['operations']);
      $details = [];
      foreach ($operations as $op => $count) {
        $details[] = $op . ' (' . $count . ')';
      foreach ($results['operations'] as $item) {
        // Default fallback to maintain backwards compatibility:
        // if "type" not given, previous message is displayed,
        // otherwise we display a completely custom one.
        if (!array_key_exists('type', $item)) {
          $message = static::translate('Action processing results: @operation (@count).', [
            '@operation' => $item['message'],
            '@count' => $item['count'],
          ]);
          $item['type'] = 'status';
        }
      $message = static::translate('Action processing results: @operations.', [
        '@operations' => implode(', ', $details),
        else {
          $message = new FormattableMarkup('@message (@count)', [
            '@message' => $item['message'],
            '@count' => $item['count'],
          ]);
      static::message($message);
        }
        static::message($message, $item['type']);
      }
    }
    else {
      $message = static::translate('Finished with an error.');
+40 −15
Original line number Diff line number Diff line
@@ -305,7 +305,7 @@ class ViewsBulkOperationsActionProcessor implements ViewsBulkOperationsActionPro
      $batch_list = $list;
    }

    $this->view->setItemsPerPage($batch_size);
    $this->view->setItemsPerPage(0);
    $this->view->setCurrentPage(0);
    $this->view->setOffset(0);
    $this->view->initHandlers();
@@ -327,7 +327,7 @@ class ViewsBulkOperationsActionProcessor implements ViewsBulkOperationsActionPro
    // Modify the view query: determine and apply the base field condition.
    $base_field_values = [];
    foreach ($batch_list as $item) {
      $base_field_values[] = $item[0];
      $base_field_values[$item[0]] = $item[0];
    }
    if (empty($base_field_values)) {
      return 0;
@@ -373,11 +373,11 @@ class ViewsBulkOperationsActionProcessor implements ViewsBulkOperationsActionPro
      // Check if the current language is on the list.
      $found = FALSE;
      $entity = $this->viewDataService->getEntity($row);
      foreach ($batch_list as $delta => $item) {
      foreach ($batch_list as $key => $item) {
        if ($row->{$base_field} === $item[0] && $entity->language()->getId() === $item[1]) {
          $this->queue[] = $entity;
          $found = TRUE;
          unset($batch_list[$delta]);
          unset($batch_list[$key]);
          break;
        }
      }
@@ -460,19 +460,25 @@ class ViewsBulkOperationsActionProcessor implements ViewsBulkOperationsActionPro
    foreach ($this->queue as $delta => $entity) {
      $accessResult = $this->action->access($entity, $this->currentUser, TRUE);
      if ($accessResult->isAllowed() === FALSE) {
        $message = $this->t('Access denied');
        $result = [
          'message' => $this->t('Access denied'),
          'type' => 'warning',
        ];

        // If we're given a reason why access was denied, display it.
        if ($accessResult instanceof AccessResultReasonInterface) {
          $reason = $accessResult->getReason();
          if (!empty($reason)) {
            $message = $this->t('Access denied: @reason', [
            $result = [
              'message' => $this->t('Access denied: @reason', [
                '@reason' => $accessResult->getReason(),
            ]);
              ]),
              'type' => 'warning',
            ];
          }
        }

        $output[] = $message;
        $output[] = $result;
        unset($this->queue[$delta]);
      }
    }
@@ -480,30 +486,49 @@ class ViewsBulkOperationsActionProcessor implements ViewsBulkOperationsActionPro
    // Process queue.
    $results = $this->action->executeMultiple($this->queue);

    // Prepare for the next major change: type hinting.
    if ($this->action instanceof ViewsBulkOperationsActionInterface) {
      // Prepare for the next major change: type hinting.
      $deprecated = FALSE;
      if (!is_array($results)) {
        $deprecated = TRUE;
        $results = [];
      }
      else {
        foreach ($results as $result) {
          if (!$result instanceof MarkupInterface) {
        foreach ($results as &$result) {
          if (!is_array($result) && !$result instanceof MarkupInterface) {
            $deprecated = TRUE;
            break;
          }
          if (is_array($result) && !($result['message'] instanceof MarkupInterface)) {
            $deprecated = TRUE;
          }
        }
      }
      if ($deprecated) {
        @trigger_error(sprintf('The executeMultiple method of the %s class must return an array of \Drupal\Component\Render\MarkupInterface, other return types are deprecated.', E_USER_DEPRECATED));
        @trigger_error(sprintf('The executeMultiple method of the %s class must return either an array of \Drupal\Component\Render\MarkupInterface or an array of arrays containing "message" (\Drupal\Component\Render\MarkupInterface) and "type" (string), other return types are deprecated.', E_USER_DEPRECATED));
      }

      // @todo Don't delete the following when removing deprecated code.
      foreach ($results as &$result) {
        if (!is_array($result)) {
          $result = [
            'message' => $result,
          ];
        }
        if (!array_key_exists('message', $result)) {
          throw new \Exception(sprintf('Result message not provided by the %s action.', $this->action->id()));
        }
        $result['message'] = (string) $result['message'];
      }
    }

    // Populate output.
    // Populate output if empty (for core actions).
    if (empty($results)) {
      $count = count($this->queue);
      for ($i = 0; $i < $count; $i++) {
        $output[] = $this->bulkFormData['action_label'];
        $output[] = [
          'message' => $this->bulkFormData['action_label'],
        ];
      }
      return $output;
    }
+15 −3
Original line number Diff line number Diff line
@@ -105,10 +105,22 @@ class ViewsBulkOperationsBatch {

    $batch_results = $actionProcessor->process();
    if (!empty($batch_results)) {
      // Convert translatable markup to strings in order to allow
      // correct operation of array_count_values function.
      foreach ($batch_results as $result) {
        $context['results']['operations'][] = (string) $result;
        $message = $result['message'];
        $found_delta = NULL;
        foreach ($context['results']['operations'] as $delta => $item) {
          if ($item['message'] === $result['message'] && $item['type'] === $result['type']) {
            $found_delta = $delta;
            break;
          }
        }
        if ($found_delta !== NULL) {
          $context['results']['operations'][$found_delta]['count']++;
        }
        else {
          $result['count'] = 1;
          $context['results']['operations'][] = $result;
        }
      }
    }
    $context['sandbox']['processed'] += $count;