1

I am working on replacing our payment system from D7/DC1 to D8/DC2 and the original code makes use of hooks. I have replaced some of them with events but I cannot find any information on the DC2 versions of:

mymodule_form_commerce_checkout_form_checkout_alter()
mymodule_form_commerce_checkout_form_review_alter()
mymodule_form_commerce_checkout_complete()
mymodule_form_commerce_cart_add_to_cart_form_alter()

Are they still the same or much like the former cart (add, update, remove) hooks, these are also events but I can't locate documentation on them. Anyone know the equivalent events or where to find the docs on these (or their replacements)

/* Small Update */

I found OrderEvents and I think I need ORDER_PAID to replace mymodule_form_commerce_checkout_complete()

Does this look correct?

I added OrderEvents::ORDER_PAID => 'orderPaid' to my getSubscribedEvents(){} array and have:

public function orderPaid(OrderEvent $event){
    $order = $event->getEntity();
    //Some internal logic here
    $order->save();
  }

//Updated Tried a hook form alter and ran through my checkout process and nothing is showing in the logs

function srs_hook_form_alter(&$form, $form_state, $form_id){
  \Drupal::logger('hook_form_alter fires')->info('<pre><code>' . print_r($form_id, TRUE) . '</code></pre>');

}
1
  • 1
    Those hooks are actually implementing hook_form_FORM_ID_alter. Which allows you to run code when a specific form is encountered/loaded. You could temporarily implement a more global hook, like hook_form_alter and then kint($form_id); to find the ID of those forms.
    – No Sssweat
    Jul 16 '20 at 0:24
2

as you've discovered, you can look in any module's src/Event/*Events.php file to find a list of their events. Using the order paid event is one way to react once you know that payment has been collected, but note that in scenarios where an order is placed with only a payment authorization (and not a capture), that event would not have been fired yet.

That may be fine in your case - if not, I'd recommend the commerce_order.place.pre_transition or commerce_order.place.post_transition events that are triggered when the order is placed. That event is coming from the state machine module and correlates to order workflow transitions. You can read more about it in the module's readme.

As for form alters, those exist in D8 as they did in D7.

2
  • I tried a generic hook form alter but it doesn't seem to fire. My module is called srs. Any ideas what I am doing wrong? See updated code Jul 16 '20 at 14:59
  • @WallyKolcz the word hook needs to be replaced with your module's name; thus, needs to be srs_form_alter(...
    – No Sssweat
    Jul 17 '20 at 1:26

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.