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 stagestate e.g. PENDING. There is a custom stagestate/transition yml which i have created. The PENDING
Which state is to be configured in a custom settings formtransition should trigger an invoice creation, for my custom modulei want to make this configurable. Therefore
So, the useradmin should be able to add one or moreselect specific states, in a fieldcustom settings form, and the invoice creationsystem should only be donecreate invoices when an order enters to one of these states.
I need to create an event subscriber, which is dynamic according to the settings that the admin has entered.
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. So, instead of
$events = ['commerce_order.place.post_transition' =>'onTriggerInvoiceCreation'];
i should do something like the following
$invoice_creation_states = explode(",", $this->config_factory->get("my_custom_module.settings")->get("invoice_creation_states"));
for($i=0;$i<count($invoice_creation_states);$i++{
$events = ['commerce_order.'.$invoice_creation_states[$i].'.post_transition' =>'onTriggerInvoiceCreation'];
As this function is static, i dontdon't seem to be able to useget hold of the config factory service in order to get the $invoice_creation_states. So
So next thought is to create a more generic event listener, for all order state changes, and in the corresponding listener callback e.g. onTriggerInvoiceCreation i could get configurationthe custom settings and make the appropriateabove condition in order to execute or not the corresponding event listener functioninvoice creation code.
Question: Is there a more generic transition event than commerce_order.TRANSITION_ID.TRANSITION_PHASE
something like
commerce_order.ANY_TRANSITION.ANY_PHASE ?