Skip to main content
1 of 4

Drupal 8 commerce, subscribe to any event

I am trying to create a dynamic event subscription, for drupal8 commerce order state.

My use case is the following. I want to create an invoice for any order which goes into stage e.g. PENDING. There is a custom stage/transition yml which i have created. The PENDING state is to be configured in a custom settings form, for my custom module. Therefore the user should be able to add one or more states in a field, and the invoice creation should only be done when an order enters one of these states.

In terms of code, in the event subscriber, i have the following code

public static function getSubscribedEvents() { $events = ['commerce_order.place.post_transition' => 'onTriggerInvoiceCreation']; return $events; }

public function onTriggerInvoiceCreation(WorkflowTransitionEvent $event) { $invoice_creation_states = explode(",", $this->config_factory->get("my_custom_module.settings")->get("invoice_creation_states")); $current_state = $event->getToState()->getId();

if(in_array($current_state, $invoice_creation_states)) {
  //create the invoice
}

}

So, a far as i understand, i need to do one of the following

In function getSubscribedEvents, i read the my_module.settings from configuration and find a way to construct the correct event subscriber.

As this function is static, i dont seem to be able to use config factory service. So next thought is to create a more generic event listener, for all order state changes, and get configuration settings and make the appropriate condition in the corresponding event listener function.

Question: Is there a more generic transition event than commerce_order.TRANSITION_ID.TRANSITION_PHASE

something like

commerce_order.ANY_TRANSITION.ANY_PHASE ?