14

How do I add an additional submit handler? I have tried this but it did not work.

function c_coupon_form_alter(&$form, $form_state, $form_id) {
  drupal_set_message($form_id);
  switch($form_id) {
     case 'commerce_checkout_form_review':
      drupal_set_message($form_id . '_form submit');
      $form['#submit'][] = 'c_coupon_review_submit';
  }
}

function c_coupon_review_submit($form, &$form_state) {
  drupal_set_message('submitted');
}
13

What you are doing is correct.

The problem is that Drupal Commerce has defined the submit action on the buttons that is clicked, which will be used instead of the submit handlers defined on the form itself. So you need to add your submit handlers to the buttons (Cancel, Continue) that you want to use your submit function.

$form['buttons']['continue']['#submit'][]='c_coupon_review_submit';
  • so how can I add the submit handler to the Continue button? – Ahmad Aug 17 '11 at 9:15
  • 1
    I believe it's $form['buttons']['continue']['#submit'][] = .. You can inspect the form variable and find out. – googletorp Aug 17 '11 at 11:08
  • But I want that my submit function should execute first than its own submit function? How could I do this? – Ahmad Aug 17 '11 at 11:13
  • That's a new question. Comments is not meant for asking/answering questions. – googletorp Aug 17 '11 at 11:15
6

To quickly answer Ahmad's refined question - how to add his custom submit function BEFORE other submit functions:

if ( empty($form['buttons']['continue']['#submit']) ) {
    $form['buttons']['continue']['#submit'] = array(); // just in case
}
array_unshift($form['buttons']['continue']['#submit'],'c_coupon_review_submit');
  • For reference, the refined question is in the comments of the answer from googletorp. – rooby Oct 8 '14 at 23:00
1

Use the following code to add extra submit handler to a form. It worked for me:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
        if ($form_id == 'xyz') {
            $form['#submit'][] = 'webform_extra_submit_test';
        }
    }

function webform_extra_submit_test($form, &$form_state) {
    global $user;
    $user_id = $user->uid; 
    // do code here $form_state['values'] etc...
}

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.