views:

246

answers:

3

Hi,

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.

+4  A: 

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;
  }
}
hfidgen
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.
joe_h
heh np - i've made an edit there, does that help?
hfidgen
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.
Henrik Opel
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.
hfidgen
+4  A: 

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.

Henrik Opel
A: 

It is best to do this before u 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 u need to use.

Suchi