7

I'm trying to alter the login form with the following code:

function git_form_alter(&$form, $form_state, $form_id) {
  $forms = array(
    'user_login_block' => 'user-login-form',
    'user_login' => 'user-login'
  );
  if (array_key_exists($form_id, $forms)) {
       //my altering code
    }
}

I got this from the openidselector module that introduces some JavaScript widgets and more or less is what I'm trying to do.
When I go to mysite/user/login it doesn't call the block where I have to alter the form. It simply doesn't call hook_form_alter().

A different thing is with the register form, or any other form, that are calling the hook.

Can anyone tell me what can I do? How is it possible that openidselector uses the same method, but in that module the code works, while it doesn't work in my code?

6

When I go to mysite/user/login it doesn't call the block where I have to alter the form. It simply doesn't call hook_form_alter().

example.com/user/login doesn't show the user login block; it shows the login form. Your code should work, even if I would rather make the code simpler (if you are not using "user-login-form" in any way).

function git_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    // …
  }
}

As for the reason why the code doesn't work, I can think of the following reasons:

  • The hook name doesn't match the short name used for the module; for example, the short name of the module is "git_form" and you are using git_form_alter() for the implementation of hook_form_alter() when it should be git_form_form_alter().
  • You are editing a module that is enabled. In this case, when you add a new hook to the code, you also need to flush the cache used by Drupal as Drupal caches the list of hooks implemented by third-party modules.
  • The module is not enabled.
  • in the info I've defined name = GIT and the sort name is git. the problem was this: I was adding a new hook with the module enabled :) Clearing the cache didn't solve it, I had to disable it and enable it again . thanks so much – PartySoft Sep 21 '11 at 15:19
  • Thanks for pointing out that I had to Clear cache I was searching why it wasn't working and then I found your answer +1! – FLY Jul 26 '13 at 6:50
0

Your code is fine, and it works ok. At least this part that you've shown.

I assume that you've put it in 'git' module, and the module is enabled? Do you have any code (there or in any other place) that could break the flow and stop your hook from executing?

  • yes my module is enabled and is called git. I have other functions in it that work fine. – PartySoft Sep 21 '11 at 15:11

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.