hook_form_alter not working
Asked Answered
I

4

5

Im using drupal 6.16. The below code for hook_form_alter is not working. Im simply trying to change the 'Log In' to 'Sign In' on the submit button of the user login form

<?php
//$Id$

function helloworld_form_alter($form_id,&$form) {
  switch ($form_id) {

      case 'user_login_form':

    // Change 'Log in' to 'Sign in'.
    $form['submit']['#value'] = t('Sign in');


      break;
  }
}

Any way to fix this ?

Please help. Thank You.

Ivelisseivens answered 10/5, 2010 at 16:10 Comment(0)
L
6

There are two errors in your code:

  1. Your function signature is wrong, as already pointed out by hfidgen (+1). It needs to be yourModuleName_form_alter(&$form, &$form_state, $form_id), so in your example the switch on the form id will never trigger.
  2. You check for the wrong form id. There are two form ids you need to check in this case, and both are different from the one you're using:
    1. user_login_block for the small login form available as a block (commonly used on most pages)
    2. user_login for the explicit login page (usually found under 'user/login')

Both forms are mostly identical in structure, so you can usually change both within the same hook_form_alter implementation - just add another case statement to check for the second version.

Lota answered 10/5, 2010 at 17:16 Comment(1)
It seems to be necessary to clear caches if the hook_form_alter is created after module installation.Bilbo
M
5

I find it easier to use theme functions to alter the forms - in your theme's template.php just create this:

function YOURTHEMENAMEHERE_user_login_form($form) {
    $form['submit']['#value'] = t('Sign in');
    //dsm($form);
    return drupal_render($form);
}

the commented out line (dsm) is for the Drupal devel module - which I'd also recommend installing. Once you've installed this and set permissions on your admin role so that you can use it, you'll get a new tab which shows you exactly how the page is constructed and which arrays do what.

Follow the trail within the arrays and you can pretty much theme anything on your site.

EDIT - oh ok :P The one thing I notice, not having used this hook before, is that the example in the API has 3 variables in the function, but you've got 2! Having a mismatch means you're probably being fed the wrong variable:

function modulename_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'contact_mail_page':
    $form['submit']['#value'] = t('Sign in');
    break;
  }
}
Minion answered 10/5, 2010 at 16:27 Comment(4)
Thanks for the reply hfidgen. Well,my aim is not to change the text of the log in button. I was doing that just to make sure that form_alter was being called. So, i do need to write code for form_alter.Ivelisseivens
The fixed signature should help (+1), but he also needs to fix the actual form ids he checks for - see my separate answer for this.Lota
Nice one. StackOverflow is so much better than the Drupal.org forums for support. If he'd asked this question there there would have been 0 answers.Minion
@hfidgen: why do you override the contact_mail_page's submit button? The OP wanted to override the user login form's submit button.Assessment
L
3

For such a trivial change, you should not write a module. The price you pay in terms of performance hit and time wasted is simply to high for the target goal.

You can perform a string replacement that will affect string that are handled by the t() function. This is done is the settings.php configuration file of the site.

Here is how you could replace "Log In" with "Sign In" ...

$conf['locale_custom_strings_en'] = array(
  'Log In'      => 'Sign In',
);

This will affect the English strings only. Feel free to replace the trailing _en with a specific language code (_fr, _ja, _es) to do the same for other languages.

Literally answered 15/7, 2011 at 17:40 Comment(0)
L
0

It is best to do this before you start any form alter: Look at the source code of your form and check for the value of the hidden field form_id - this gives you the exact form_id that you need to use.

Legumin answered 12/5, 2010 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.