Creating a new action

Last updated on
28 September 2022

To create a new action, create a new module (the only required file is some_module.info.yml) and place the action class file in the src/Plugin/Action folder.

The file must contain annotation and the action class itself.

1. Annotation

Example annotation for a Views Bulk Operation Action plugin:

/**
 * Some description.
 *
 * @Action(
 *   id = "some_module_some_action",
 *   label = @Translation("Some action label"),
 *   type = "",
 *   confirm = TRUE,
 *   requirements = {
 *     "_permission" = "some permission",
 *     "_custom_access" = TRUE,
 *   },
 * )
 */

This is actually a core Action plugin but there are some changes in annotation managed by the extension of the default ActionManager:

  1. type: can be left empty, this way the action will be applicable to all entity types,
  2. confirm: if set to TRUE, the confirm_form_route_name parameter will be set to Views Bulk Operations default action confirmation form, providing the action with a simple confirm step. If the confirm_form_route_name parameter is set however the confirm setting will not take effect as the set confirm_form_route will be used instead.
  3. requirements: an array of requirements that must be met to make the action available. Possible options:
    1. _permission - if user has that permission, the action will be displayed in the options select list.
    2. _custom_access - if set to TRUE, action's customAccess method will be called to determine if the current user can run the action on a specific object/ entity (see ViewsBulkOperationsActionBase for a default implementation). If FALSE or not defined, the customAccess method will not be called. 

If requirements are not defined or empty, the action will be shown to every user that can access the view.

2. Action class

To have a set of predefined methods for advanced functionalities it's best to extend the ViewsBulkOperationsActionBase class when creating your action. There are also two methods that have to be implemented:  access and execute or executeMultiple (executeMultiple is required by ActionInterface but has a default implementation in ViewsBulkOperationsActionBase that calls $this->execute in a loop for all entities).

<?php

namespace Drupal\some_module\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Action description.
 *
 * @Action(
 *   id = "some_module_some_action",
 *   label = @Translation("Some action label"),
 *   type = ""
 * )
 */
class SomeAction extends ViewsBulkOperationsActionBase {

  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    // Do some processing..

    // Don't return anything for a default completion message, otherwise return translatable markup.
    return $this->t('Some result');
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    // If certain fields are updated, access should be checked against them as well.
    // @see Drupal\Core\Field\FieldUpdateActionBase::access().
    return $object->access('update', $account, $return_as_object);
  }

}

Enable the new action

Views Bulk Operations module defines a new Views bulk operations field plugin, available under view settings -> Fields and categorized as "Global" for any entity type view. The module doesn't affect the core Node operations bulk form field plugin in any way. Newly created actions are available for the Views bulk operations field plugin only.

To enable the new action for a View, edit the View, under Fields click on "Global: Views bulk operations (Views bulk operations)", under "Selected actions", check the checkbox next to your new action, save the field, and save the View.

Next: Configuration

Help improve this page

Page status: No known problems

You can: