This page contains useful notes and code snippets for working with hook_form_alter(). See also the API documentation for example_form_alter() and the Form API Quickstart Guide.
Identifying the form ID
Via HTML
The IDs of form elements can be found by viewing a page's HTML. Search the HTML for "form_id" to find the correct form ID.
Using hook_form_alter() to find a form ID
If you want to alter one form on your site and not every form, you will first need to identify the form's form ID. The following examples assume that you are familiar with creating a module. If this is not the case, review the Module developer's guide. hook_form_alter() needs to be called from within a custom module.
In your custom module, create a function called modulename_form_alter().
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
drupal_set_message("Form ID is : " . $form_id);
}
This function will output the following when you visit a page with a form:
Form ID is : your_form_id
You will use the form ID to select and modify a particular form. Note that your page may display more than one form ID. If you have a search form on every page, this form ID will also display. Make sure that you have identified the correct form.
Using hook_form_alter with node edit, user login, and user profile forms
Node edit forms
The form ID for node edit forms is always node_type_node_form (example: 'story_node_form
').
Some variations on a theme for detecting node forms in general, on D6:
// override_node_options module
if (isset($form['type'])
&&
$form['type']['#value'] .'_node_form' == $form_id) {}
// Ubercart. Checks the type is one that we care about.
// Note that we've got hold of the $node first; this requires checking!
$node = $form['#node'];
if (is_object($node)
&&
$form_id == $node->type .'_node_form'
&&
CHECK_OUR_TYPE($node->type)) {}
// image module
// any node type except image
if (isset($form['type']['#value'])
&&
$form['type']['#value'] == 'TYPE') {
$type = $form['type']['#value'];
// If enabled adjust the form.
if ($form_id == $type .'_node_form'
&&
variable_get('image_attach_'. $type, 0)) { }
}
if (($type = $form['type']['#value'])
&&
($form_id == $type .'_node_form')) { }
On D7 there's a simpler way:
if (!empty($form['#node_edit_form'])) {
// ....
}
User login forms
Unlike most forms, the user login form displays in two places and has two different form IDs. If you want to change both user login locations, you will need to target both 'user_login' and 'user_login_block.' user_login is the login form on the /user page. user_login_block is the login form in the user login block.
if($form_id == 'user_login' || $form_id == 'user_login_block') {
//do fun stuff here.
}
User profile forms
The user profile form ID is 'user_profile_form'. The actual form data is contained within a fieldset whose key is the raw user-entered string (e.g. Main Address). Within hook_form_alter, look at at $form['_category']['#value'] to tell which category you are currently on and to find the key of this fieldset.
Debugging hook_form_alter
hook_form_form_id_alter not overriding a form
hook_form_form_id_alter() can be used when you are overriding a single form. Be advised that hook_form_form_id_alter() will be called before hook_form_alter. As a result, you may not be able to alter something another module has altered.
Another module's hook_form_alter taking precedence
Module weights determine the order in which different modules can alter any form. If you wish to alter elements in a form that has been supplied by another module, you may have to increase the weight of your own module to make it run later.
Comments
if this were cleaned up a
if this were cleaned up a bit, it can be added to the main part of the handbook page above.
I'm posting this to help others looking for examples about using FAPI to make a add user form. Also hoping someone who knows more than I will look at it and suggest alternative (better) ways of doing this.
Had to hack a bit of genpass module. is there a way I can avoid that?:
diff genpass.module themodule.orig
84,85d83
< case 'myaddaleader_form':
< case 'myaddamember_form':
mymodule (yes, I called it mymodule)
no screenshot jpgs, but a quick tour of the forms: http://cathytheys.blip.tv/file/3421680
how to validate form field element before submission
I want to register new users but I want to validate some of the fields eg "club id" to prevent anybody who is not a club member from registering unless you have an id.
I want the field input to follow a pattern for example AB/XYZ/10/0001 before
the new user will be registered[a legitimate member already knows his/her id].
Is content profile module a right choice if so how?
use Drupal d_printr() function instead of PHP print_R()
Use d_printr() to dump the form array values with better formatting
~are you netsperienced?
♥ follow me @decibelplaces ∞
user login form example lacking
It would be really helpful if the user login form example had some real example code in there, besides just //do fun stuff here.
Anyone want to expand upon that please and thanks?
I'm looking for a way to include the words "username" and "password" as default text in the user login form fields. Thanks!
- Kendall Totten
Re: user login form example lacking
A little bit late... This is some fun stuff you can do ;)
how do I can use this in fild
how do I can use this in fild with radio buttons widget?
$form['my_field']['und'][0]['value']['#description'] = t('Check please.');
Help