Replace default submit handler when using drupal_get_form() in D7 I'm having issues calling an existing form from the commerce_coupon module which runs a default submit handler callback (commerce_coupon_form_submit).

My form builds off of that form and simply tries to replace the default '#submit' value to call my own submit function instead of the default commerce_coupon one. I'm using drupal_get_form so my code looks like the following:

$form = drupal_get_form('commerce_coupon_form', $coupon);
$form['#submit'] = array('mymodule_coupon_submit');
return drupal_render($form);


That is the code from inside a function that is supposed to return the rendered form. The problem is the default commerce_coupon_form_submit submit handler is still running and my own function doesn't get called. I think this has something to do with the form_id or form_state still being commerce_coupon_form.

I've found related problems on stackexchange but responses were directed for other versions of Drupal. I'm using D7 so please bear that in mind.

I'm adding a little more detail that may help get a better answer for this. The original commerce_coupon_form seems to try to use a button-level #submit handler as there is the following before the form is returned:

$form['actions'] = array(
    '#type' =&gt; 'container',
    '#attributes' =&gt; array('class' =&gt; array('form-actions')),
    '#weight' =&gt; 400,
  );
  $form['actions']['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Save coupon'),
    '#weight' =&gt; 40,
  );


but the actual #submit handler isn't assigned so the default drupal handler is being set at the root of the form. I've tried assigning a button-level submit and overriding the root drupal assigned submit to a blank array but that doesn't seem to work. I'm trying to track down documentation for drupal or the function that sets the default #submit so I can figure this out. If you can point me in the right direction I would appreciate it.
The problem is when the form builder builds the form up again when it's submitted, your function won't be called so it doesn't know about the changes you've made.

hook_form_alter() (and hook_form_FORM_ID_alter()) were written for exactly this purpose, I'd advise using one of those, e.g.

function MYMODULE_form_commerce_coupon_form_alter(&amp;$form, &amp;form_state, $form_id) {
  $form['#submit'] = array('mymodule_coupon_submit');
}


It's worth bearing in mind that other modules' hooks might run after yours and change that array again. To avoid that have a look at How to update a module's weight.