drupal-8drupal-viewsbulk-operations

How can I enhance a Drupal 8 Views Bulk Operations Confirm Page?


For a custom Views Views Bulk Operations action, I would like to enhance the information in the list on the confirm page, For example, instead of:

I would like to have:

Where is the best place to alter this?


Solution

  • There are basically two ways to do this in Drupal 8:

    1. using hook_form_views_bulk_operations_confirm_action_alter. This will allow you to alter the list, for example with custom values and code.
    2. If you have defined a custom action plugin, then in the plugin annotiation you can declare the route name to a custom validation form. This will allow you to do anything you want, including multi-step forms. When you define that custom form, you should subclass Drupal\views_bulk_operations\Form\ConfirmAction, since it's buildForm method takes parameters additional to that of a regular form. So your Plugin would start like this:
    /**
    * Action description.
    *
    * @Action(
    *   id = "my_special_action",
    *   label = @Translation("My Special Action"),
    *   type = "custom_entity",
    *   confirm_form_route_name = "my_module.my_special_action_confirm_form",
    * )
    */
    class MySpecialAction extends ViewsBulkOperationsActionBase {
    }
    

    and the Form will look something like this:

    use Drupal\views_bulk_operations\Form\ConfirmAction;
    
    class MySpecialActionConfirmForm extends ConfirmAction {
    
      public function getFormId() {
        return 'my_special_action_confirm_form';
      }
    
    
      public function buildForm(array $form, FormStateInterface $form_state, $view_id = NULL, $display_id = NULL) {
        ....
      }
    

    In the custom form class, you will have to define your own submitForm method, if you want to pass anything special to the custom action.