Search code examples
drupaldrupal-7

hook_form being called twice


hook_form being called twice

/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register_form') {
    $form_state['redirect'] = false;
    my_function();      
  }
}

my_function() calls twice and I need only one.

I read this post:

https://drupal.stackexchange.com/questions/22881/hook-form-being-called-twice

And I try use $form_state['redirect'] = false but don't work forme.

Anyone know any clean way to do it? Thanks


Solution

  • It looks to me like you should be doing whatever it is you doing in your own submit function.

    Add your own submit function by adding your function name to the submit array in your form_alter.

    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
      if ($form_id == 'user_register_form') {
        $form['#submit'][] = 'MYMODULE_submit_function';    
      }
    }
    
    function MYMODULE_submit_function(&$form, &$form_state) {
      $form_state['redirect'] = false;
      my_function(); 
    }