1

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"); } 
6
  • 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? Commented Nov 7, 2016 at 17:42
  • hook_form_alter is firing twice . if i dpm("test") inside hook form it display twice. Commented Nov 7, 2016 at 18:02
  • Can you run dpm($form_id); inside your hook_form_alter(&$form, $form_id), do you get different form ID's? Commented Nov 7, 2016 at 18:09
  • its giving me same form_id Commented Nov 7, 2016 at 18:37
  • Is your form being outputted more than once on the page you are on? How are you rendering the form? Commented Nov 7, 2016 at 18:42

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.