0

I have a module where I implemented hook_form_alter:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  krumo($form_id);
}

And I always get each form_id displayed twice. Duplicate form_id for each form!! The problem is that if I add in hook_form_alter line like:

$form['#submit'][] = 'my_submit_function';

then this function is getting called twice, and this follows errors. I have no idea why Drupal calls hook_form_alter implementation twice, possibly you have. Thank you in advance!

2
  • Is the form_alter called twice only when you submit the form? Nov 15, 2012 at 22:41
  • Does it happen for all the forms, or just one?
    – apaderno
    Nov 19, 2012 at 17:30

4 Answers 4

3

I ran into the same problem today, a hook_form_alter() override in a custom module was being called twice on every form.

My custom module's name was "project_name" and my theme was also called "project_name" (folder/machine names). I uninstalled my custom module and renamed it to "project_name_different" and re-installed it. After that change, hook_form_alter() only gets called once.

Don't know the super technical explanation for this answer, but generally Drupal doesn't like duplicate names across modules and themes, so make sure machine names are unique across all modules and themes living within your site.

2

I believe that hook_form_alter() will be called once for each form on the page. So, if there is a second form, possibly in a block or added by another module, that form will also be processed through hook_form_alter().

7
  • actually I thought it was clear from the question that I get the same form_id twice. for instance krumo of user_login_block on a home page is displayed twice.
    – nakajuice
    Aug 13, 2012 at 15:00
  • Ok I see. Is it possible that you changed the name of your module and Drupal is still picking up an old version's hook_form_alter implementation? Aug 13, 2012 at 15:02
  • No, I didn't. Module wasn't renamed.
    – nakajuice
    Aug 13, 2012 at 15:06
  • What else does your module do? Could you post more code? Aug 13, 2012 at 15:09
  • 1
    Great! Please post the problem that you found and your solution when you solve it. Aug 13, 2012 at 15:28
0

Probably you are giving to your module and theme the same name!

1
  • 1
    You answer is a bit short, can you expand any more on this? Can one even have two modules/themes with the same name enabled?
    – Letharion
    Nov 19, 2012 at 18:24
0

Note that when your custom module and your theme have the same name, hook_form_FORM_ID_alter will be called twice.

1
  • Why are you answering a 2 year old question (with the same answer)?
    – 2pha
    Aug 27, 2014 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.