0

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.

3
  • What is the question? We need one to answer.
    – cilefen
    Apr 7 at 3:36
  • Does this Help ? drupal.stackexchange.com/questions/295329/… Apr 7 at 12:54
  • @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.
    – liquidcms
    Apr 7 at 14:34

1 Answer 1

2

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

1
  • 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.
    – liquidcms
    Apr 8 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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