In the following code custom call back function is being printed twice. How to prevent submit call back function being called twice.

function my_module_form_alter(&$form, &$form_state, $form_id){
 if ($form['#id'] == "views-exposed-form-test-page"){
    $form['#submit'][] = 'custom_callback';

function custom_callback($form,&$form_state){
    $form_state['redirect'] = false;
    dpm("insert record");
  • is hook_form_alter() firing twice resulting in the custom callback firing twice? Or is the hook_form_alter() being fired once, and the custom function is firing twice?
    – DMAC
    Nov 7 '16 at 17:42
  • hook_form_alter is firing twice . if i dpm("test") inside hook form it display twice. Nov 7 '16 at 18:02
  • Can you run dpm($form_id); inside your hook_form_alter(&$form, $form_id), do you get different form ID's?
    – DMAC
    Nov 7 '16 at 18:09
  • its giving me same form_id Nov 7 '16 at 18:37
  • Is your form being outputted more than once on the page you are on? How are you rendering the form?
    – DMAC
    Nov 7 '16 at 18:42

hook_form_alter() will fire once for every time there is a form on the current page.

You can try the hook hook_form_FORM_ID_alter(). This will only run once when the form with the specific ID pops up.

Unfortunately I had this issue happen before, the hook_form_alter() ran multiple times. I stopped the code from running multiple times by encapsulating the code inside an IF statement inside the hook_form_alter() example: if($form_id == my_id). So the hook ran multiple times but the code only ran once.

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.