0

How will a predefined form behave if we have two different forms that implement hook_form_alter?

I would explain my question by an example, because currently I got a crash on it:

I have a predefined user_login form in user.module

I created two new blocks called author_login and creator_login. Each of these module using the form user_login and adding new validating properties for each form by using function hook_form_alter().

The new validating properties are:

  • For author_login block: if user doesn't have author role, user will not be logged into system, error: "you don't have author role"

  • For creator_login block: if user doesn't have creator role, user will not be logged into system, error: "you don't have creator role"

But the problem is: the new login block I created works fine if only one of two modules is enable. If I enable both modules, no user cannot log in to the system through these module.

For example, I have both modules enabled, I go to author_login block and log an user named Mark, who has role Author, to the system, an error message displayed: "the user Mark doesn't have creator role". And similar to creator_login block.

Why does this happen? I thought that the hook_form_alter will alter different properties to each new module we created, make each new form works separately. But from this case I encountered, it seem like the user_login form validate all new properties that I added by using hook_form_alter. And how can I avoid this as I want these two form to work separately?

0

It sounds like you're altering the same form to provide the additional validation so it is expected that both the author and creator validation checks will be carried out when both modules are enabled.

If you want to continue using the same form for both blocks then I would suggest wrapping the code within your validation callbacks to check which form you are currently validating.

  • Could you please explain in more details? Because I'm pretty new to Drupal so I hope you can give me some direction. Thanks in advanced! – Trung Bún Sep 28 '14 at 13:14
  • Can you provide your code? – ratface Sep 29 '14 at 20:03

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.