2

There is a custom entity called organisation. I am trying to build a view to allow bulk delete of the entities.

I checked the view for display content and there is a field to display the checkbox(Node operations bulk form).

This field doesn't show up for the custom entity. Enabling the actions module does not help.

Adding a custom action for delete organisation does not help either. The new action shows up on the list of actions, but the "operations bulk form" does not show up on views for the entity.

Thanks in advance for your help

3 Answers 3

6

You need both an action plugin and an action config entity

  1. Create the plugin with drush: drush generate plugin-action
  2. Create the action config entity: look at "system.action.comment_delete_action.yml" for an example.

After creating these, clear the cache and it should be available in views under the label "Bulk update".

2
  • I created my action plugin using drush (drush generate plugin-action), then I had to create a config like "system.action.comment_delete_action.yml"
    – mpp
    Jul 2, 2018 at 15:26
  • You don't need a config entity if you have Views Bulk Operations installed.
    – mbomb007
    Apr 24 at 15:57
1

It looks like as of 8.6.x, this should be possible by just specifying the delete-multiple-form link template and the delete-multiple-confirm form class for the entity type. For the form class, you can use the generic Drupal\Core\Entity\Form\DeleteMultipleForm.

Not managed to get it to work yet though!

4
  • 1
    Ah, you also need at least one action config entity as well. See Media module for an example of the delete action which uses the derived plugin.
    – joachim
    Jan 17, 2019 at 11:54
  • do you have a code example I'm getting lost in the media module Feb 4, 2021 at 17:22
  • I don't, but IIRC if you generate an entity type with Module Builder it'll produce the code you need.
    – joachim
    Feb 10, 2021 at 10:39
  • Creating the annonations suggested in this post and a config entity like "system.action.media_delete_action.yml" works. Another example : drupal.org/project/drupal/issues/2864689
    – Rufein
    Apr 6, 2021 at 16:03
0

After reading some tutorials and analyzing drupal code I figured out how to get this done in Drupal 9 or 10.

Here is a more detailed description what I have done:

How To Add Bulk Actions to your Custom Entity

Therefore, it is necessary to create different items in Drupal 9/10 as code:

  • Plugin Field Bulk Action Form: adds possibility to have checkboxes and action dropdown in your view (until now action dropdown is empty)
  • Plugin Action ActionBase: add functionality to action dropdown entry
  • config files install/system.action.YOUR_ACTION.yml and scheme/YOUR_NAME.scheme.yml: connect bulk form and ActionBase-Class via file and field names
  • add an update hook into your module to activate new schema

Plugin Field "Bulk Action Form"

  • path: my_module/src/Plugin/views/field/MyBulkForm.php
  • must extend from Drupal\views\Plugin\views\field\BulkForm
  • id my_bulk_form in comment @ViewsField is necessary for later usage
  • example code:
    <?php
    
    namespace Drupal\my_module\Plugin\views\field;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\views\Plugin\views\field\BulkForm;
    
    /**
     * Defines a user operations bulk form element.
     *
     * @ViewsField("my_bulk_form")
     */
    class MyBulkForm extends BulkForm {
      /**
       * {@inheritdoc}
       */
      protected function emptySelectedMessage(): string|TranslatableMarkup {
        return $this->t('Nothing selected.');
      }
    }
    

Plugin Action "ActionBase"

  • path: my_module/src/Plugin/Action/MyAction.php
  • must extend Drupal\Core\Action\ActionBase
  • example code:
    <?php
    namespace Drupal\my_module\Plugin\Action;
    use Drupal\Core\Access\AccessResultInterface;
    use Drupal\Core\Action\ActionBase;
    use Drupal\Core\Entity\EntityStorageException;
    use Drupal\Core\Session\AccountInterface;
    use Drupal\my_module\Entity\MyCustomEntityInterface;
    
    /**
    * My Action
    *
    * @Action(
    *   id = "my_action_id",
    *   label = @Translation("My Action shown in dropdown"),
    *   type = "my_custom_entity_id"
    * )
    */
    class MyAction extends ActionBase {
    
      /**
       * @inheritDoc
       */
      public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE): bool|AccessResultInterface {
        // TODO: Implement access() method.
        return true;
      }
    
      /**
       * @inheritDoc
       */
      public function execute(MyCustomEntityInterface $entity = NULL): void {
        if (isset($entity)) {
          try {
            $entity->delete();
          } catch (EntityStorageException $e) {
            \Drupal::logger('my_module')->error("Error while deleting custom entity: " . $e->getMessage());
          }
        }
      }
    }
    

config files

  • config files somehow connect Action and Bulk Form, so in the Bulk From dropdown contains the desired action
  • config install file:
    • path to install yml file: my_module/config/install/system.action.my_action.yml
    • example code:
    langcode: en
    status: true
    dependencies:
    module:
      - my_module
    id: my_action_id # id used in annotation "id" of MyAction.php
    label: 'My Action' # text shown in dropdown
    type: my_custom_entity_id # id of your custom entity which is this action for
    plugin: my_action_id
    configuration: {  }
    
  • config schema file:
    • path: my_module/config/schema/somename.schema.yml
    • example code:
    action.configuration.my_action:
    type: action_configuration_default
    label: 'My Action'
    

Update hook

  • path: my_module/my_module.module
  • code example:
function my_module_update_9501(&$sandbox){
  $config_installer = \Drupal::service('config.installer');
  $config_installer->installDefaultConfig('module', 'my_module');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.