Skip to main content
Removed tags from title
Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Drupal 8 commerce, subscribe Subscribe to any order state transition

added 17 characters in body; edited title
Source Link

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 ?

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 ?

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 state e.g. PENDING. There is a custom state/transition yml which i have created.

Which state transition should trigger an invoice creation, i want to make this configurable.

So, the admin should be able to select specific states, in a custom settings form, and the system should only create 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 don't seem to be able to get hold of the config service in order to get the $invoice_creation_states.

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 the custom settings and make the above condition in order to execute or not the invoice 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 ?

added 17 characters in body; edited title
Source Link

Drupal 8 commerce, subscribe to any eventorder state transition

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();

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 ?

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 ?

Drupal 8 commerce, subscribe to any order state transition

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 ?

Source Link
Loading