Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hook_form_alter not working

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.

like image 612
joe_h Avatar asked May 10 '10 16:05

joe_h


3 Answers

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.

like image 53
Henrik Opel Avatar answered Nov 10 '22 07:11

Henrik Opel


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;
  }
}
like image 42
MrFidge Avatar answered Nov 10 '22 09:11

MrFidge


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.

like image 3
asiby Avatar answered Nov 10 '22 07:11

asiby