Advanced

Last updated on
14 April 2018

As seen in the Annotation section of the guide, the Batch API context and the view with selected results can be both passed to the action object and be available during its execution. This guide also shows other possibilities like methods that can be overridden or altering existing action definitions.

1. The context

Normally the action object $context parameter (accessed by $this->context) contains data needed to initialize the action processor like the view ID, display ID, etc. However,  if the pass_context annotation is set to TRUE (and always since 2.x) the $context parameter will have a merged Batch API context, so it'll also contain elements: sandbox, results, message and finished. Of those only the sandbox is passed by reference and can be modified, the remaining variables are passed during a Batch API process, so message and finished have operation initial values.

The $this->context['sandbox'] variable contains batch progress data and can be used to pass additional data to subsequent execute operations (when execute method is used) or batches (when executeMultiple method is used).

Example:

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    if (!isset($this->context['sandbox']['counter'])) {
      $this->context['sandbox']['counter'] = 0;
    }
    
    // Do some processing..
    $this->context['sandbox']['counter']++;

    if ($this->context['sandbox']['counter'] == 30) {
      drupal_set_message($this->t('We have just hit 30.'));
    }
  }

2. The view

If the pass_view annotation is set to TRUE (and always since 2.x), the action object will have the $view parameter set. The $view->result will contain the selected results that will be processed in the current batch (not all the selected results).

Example usage:

/**
 * {@inheritdoc}
 */
public function executeMultiple(array $entities) {
  foreach ($entities as $delta => $entity) {
    drupal_set_message($this->t('The result language is: @language.', [
      '@language' => $this->view->result[$delta]->node_field_data_langcode,
    ]));
    
    // Process the entity..
  }
}

NOTE: In 1.x branch when passing view to an action that alters entity properties that are included in view query filters and using batch operation, the $view->result will be corrupt. Use $view->result only with actions that don't alter such parameters and don't delete entities during batches. The 2.x branch does not have this limitation.

3. Possible overrides

There's a possibility to override the default implementations of setContext and setView of the ViewsBulkOperationsActionBase class for even more possibilities and setting the action parameters in a completely customized way.

4. Altering existing actions

The plugin.manager.views_bulk_operations_action service used by Views Bulk Operations provides a way to alter existing action definitions, however, it's done using an alter event, not an alter hook as in case of plugin.manager.action service. To alter existing action definitions, one needs to create an event subscriber class and define it in my_module.services.yml:

services:
  my_module.views_bulk_operations_actions:
    class: Drupal\my_module\EventSubscriber\MyModuleEventSubscriber
    tags:
      - { name: event_subscriber }

And the event subscriber class:

namespace Drupal\my_module\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager;

/**
 * Defines module event subscriber class.
 *
 * Alters actions to make use of permissions created by the module.
 */
class MyModuleEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ViewsBulkOperationsActionManager::ALTER_ACTIONS_EVENT][] = ['alterActions', 0];
    return $events;
  }

  /**
   * Alter the actions' definitions.
   *
   * @var \Symfony\Component\EventDispatcher\Event $event
   *   The event to respond to.
   */
  public function alterActions(Event $event) {
    // Alter any action definition properties here.
    $event->definitions['some_action']['some_property'] = 'some value';
  }

}

For a working example see the actions_permissions module included.

Next: View providers API

Help improve this page

Page status: No known problems

You can: