Score:0

I can't see custom action

us flag

I am trying to create a custom action for bulk operations on commerce orders.

I am using the following code.

mymodule/src/Plugin/Action/DeleteOrderAndApps.php

namespace Drupal\mymodule\Plugin\Action;
    
use Drupal\commerce_order\Entity\Order;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
    
/**
 * Delete Order and attached Apps action
 *
 * @Action(
 *   id = "delete_order_and_apps",
 *   label = @Translation("Delete Order and Apps"),
 *   type = "commerce_order"
 * )
 */
class DeleteOrderAndApps extends ActionBase {

  /**
   * {@inheritdoc}
   *
   */
  public function execute(Order $order = NULL) {
    // …
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = $object->access('delete', $account, TRUE);
    return $return_as_object ? $result : $result->isAllowed();
  }
    
}

I am not sure I need it, but I also have the mymodule/config/install/system.action.delete_order_and_apps.yml file.

langcode: en
status: true
dependencies:
  module:
    - commerce_order
id: delete_order_and_apps
label: 'Delete Order and Apps'
type: commerce_order
plugin: delete_order_and_apps
configuration: {  }

The confusing part is there are 2 different bulk options: VBO and core's bulk actions for Order entity. Sadly these don't use the same action definitions but my new action does not show as an option for either of these when creating a view. I have done many of these the same way but all are Node actions.

Not sure if something missing in my code or possibly something missing in Commerce that is required to allow adding custom actions (something missing in entity definition perhaps).

I am using Drupal 8.9.20 and the Commerce module 8.x-2.24.

mbomb007 avatar
nl flag
I had this issue if I used a custom deriver class. If something with the deriver doesn't work, the action doesn't show up, and in my case there were no errors, whether I used core's bulk actions or VBO. If I switch from specifying a `deriver` to specifying the `type` instead, it works.
id flag
What is the question? We need one to answer.
miststudent2011 avatar
fr flag
Does this Help ? https://drupal.stackexchange.com/questions/295329/custom-action-plugin-with-configurationform
liquidcms avatar
us flag
@miststudent2011, that posts seems to be mostly about adding configuration for an action - which isn't this question. The only part mentioned there which is missing is possibly the .schema file; but never added those for the (node) action which i do have working.
Score:2
cn flag

Assuming you're using the core "Bulk update" field, which the commerce order View uses out of the box, you need to create the action as a config entity.

You already have the config to do that, but it will only be used when the module is installed. If you're able to uninstall then install your module again, do that and you'll see your action in Views where it should be:

enter image description here

If you can't re-install the module, add an update hook in mymodule.post_update.php to create the action manually, then run database updates:

function mymodule_post_update_add_my_action() {
  \Drupal::entityTypeManager()->getStorage('action')->create([
    'id' => 'delete_orders_and_apps',
    'label' => 'Delete Order and Apps',
    'type' => 'commerce_order',
    'plugin' => 'delete_order_and_apps',
  ])->save();
}

If you're using VBO you don't need to create the config entity, the module will pick up the action without it. If that's not working, make sure everything is named correctly, caches are cleared etc, as it's definitely there:

enter image description here

mbomb007 avatar
nl flag
*If you're using VBO you don't need to create the config entity, the module will pick up the action without it.* This was super helpful.
liquidcms avatar
us flag
good to know i was doing it correctly. And i figured it was something that would only be triggered with module uninstall/re-install. I had done that yesterday before i posted this. Today i loaded a fresh copy of the db from our production site, run a config import and numerous cache rebuilds before seeing your answer and going back to look at this - and after the day's activities, it now shows up. Not sure what it was that triggered it but glad to know it works as i had expected. Thanks for the very details answer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.