| 5 core.php | hook_form_alter($form_id, &$form) |
| 6 core.php | hook_form_alter(&$form, &$form_state, $form_id) |
| 7 system.api.php | hook_form_alter(&$form, &$form_state, $form_id) |
| 8 system.api.php | hook_form_alter(&$form, &$form_state, $form_id) |
Perform alterations before a form is rendered.
One popular use of this hook is to add form elements to the node form. When altering a node form, the node object can be accessed at $form['#node'].
In addition to hook_form_alter(), which is called for all forms, there are two more specific form hooks available. The first, hook_form_BASE_FORM_ID_alter(), allows targeting of a form/forms via a base form (if one exists). The second, hook_form_FORM_ID_alter(), can be used to target a specific form directly.
The call order is as follows: all existing form alter functions are called for module A, then all for module B, etc., followed by all for any base theme(s), and finally for the theme itself. The module order is determined by system weight, then by module name.
Within each module, form alter hooks are called in the following order: first, hook_form_alter(); second, hook_form_BASE_FORM_ID_alter(); third, hook_form_FORM_ID_alter(). So, for each module, the more general hooks are called first followed by the more specific.
Parameters
$form: Nested array of form elements that comprise the form.
$form_state: A keyed array containing the current state of the form. The arguments that drupal_get_form() was originally called with are available in the array $form_state['build_info']['args'].
$form_id: String representing the name of the form itself. Typically this is the name of the function that generated the form.
See also
hook_form_BASE_FORM_ID_alter()
Related topics
44 functions implement hook_form_alter()
File
- modules/
system/ system.api.php, line 1616 - Hooks provided by Drupal core and the System module.
Code
<?php
function hook_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
$form['workflow']['upload_' . $form['type']['#value']] = array(
'#type' => 'radios',
'#title' => t('Attachments'),
'#default_value' => variable_get('upload_' . $form['type']['#value'], 1),
'#options' => array(t('Disabled'), t('Enabled')),
);
}
}
?> Login or register to post comments
Comments
D7 Newbie Notes
To modify a form, check $form['#id'] for the name of the form.
It wasn't obvious to me, but you are only supposed to modify $form.
A way to discover the name of forms is to add this line to your hook_form_alter on your dev site
hook_form_alter{
watchdog('cg_volunteer', 'cg form_alter has run %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
}
The best way to do that is
The best way to do that is with drupal_set_message, or even better with the devel module's dsm() function. Then you can do handy stuff like this:
<?phpfunction example_form_alter(&$form, &$form_state, $form_id) {
dsm($form_id); // print form ID to messages
dsm($form); // pretty print array using Krumo to messages
}
?>
That way you get it right there in the page you're looking at without having to go dig around in the watchdog. Here's the equivalent without devel (though why you wouldn't want to use devel I don't know...):
<?phpfunction example_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message($form_id); // print form ID to messages
drupal_set_message(print_r($form, TRUE)); // print array to messages
}
?>
One obvious advantage of using devel's dsm() function is you don't even have to know wether the variable is a string, array, or object - devel takes care of all that for you so you don't have to use print_r().
hooks can be called in template.php in D7
If for some reason devel is not installed:
<?phpfunction mytheme_form_alter(&$form, &$form_state, $form_id) {
$print = '<pre>' . print_r($form, TRUE) . '</pre>';
if (module_exists('devel')) {
dsm($form_id); // print form ID to messages
}
else {
drupal_set_message($form_id); // print form ID to messages
}
if (module_exists('devel')) {
dsm($form); // pretty print array using Krumo to messages
}
else {
drupal_set_message($print); // print array to messages
}
}
?>
Wrapping
print_r($form, TRUE)with<pre>tags returns the variable data in a easier to read format, although Krumo is better.Change field name of contact form
Hi 'am really a newbie to drupal. I need to change name of one of my fields in contact form. Can some one please help me?
Thanks
Amy
Hi Amy, A couple of things to
Hi Amy,
A couple of things to note.
This is not the best place to post questions as it should be for documentation and also these comments are not in the normal support forums so no-one will see your request unless they come to this page.
Consider using drupal stack exchange for answers http://drupal.stackexchange.com/ or the normal Drupal forums http://drupal.org/forum. Other than that there are many active IRC channels such as #drupal-uk where people might be able to help you.
I'm not entirely sure which contact form you are referring to?
hook_form_alter can be used to alter almost anything in any drupal form but it requires some custom code in either a custom module or add some code in template.php in your theme as stated above (Drupal 7).
Have a look at http://drupal.org/project/stringoverrides
or install http://drupal.org/project/webform module and create your own form.
easy hook_form_alter
Suppose you have created a custom module and you want to create hook_form_alter for that module. Here are some easy steps to implement and test the working of hook_form_alter.
Suppose you want to add a new field in your in your comments form , say a checkbox. You can do this using the
hook_form_alterof your custom module. Before doing this make sure that writing comment is enabled in your content type.hook_form_altertakes three parameters$form,$form_dateand$form_id. If you just want to see the working of form_alter then you need to worry about only $form_id.Just write this piece of code in your mymodule.module file:
function mymodule_form_alter(&$form, $form_state, $form_id) {switch ($form_id) {
case 'comment_form_id':
$form['your_comment_form_name'] = array (
'#type' => 'checkbox',
'#title' => t ('Subscribe to replies to this comment'),
);
break;
}
}
The name of comment form is of your choice.
If you are wondering what $form_id would in your case, then add this piece of code before
switch($form_id):drupal_set_message($form_id);You will see all the form_id's of your page after refreshing the page. Just copy the form_id of your comments form and paste it in place of comment_form_id.
Enable your module and view your comments form. You will see the checkbox in there.
Even if you dont see the checkbox in your comments form, try clearing the cache. If you have followed the above steps correctly, you will surely see the checkbox in comments form.
I hope I have made myself clear :)
Thank you Mukesh for your guidance
Glenn, 90, told an audience
Glenn, 90, told an audience barbour jacketsin Columbus the flight was the result of "more than two years of training and working with a marvelous team."
"That is why the craft was called http://www.shopbarbourjackets.com Friendship 7, because of the team," he said.
Glenn's groundbreaking flight on Feb. 20, 1962,barbour quilted jacket put the United States into a heated space race with the Soviet Union, which had launched cosmonaut Yuri Gagarin into orbit 10 months earlier.
"It was the best day of my life," said Glenn,http://www.officialbarbourshop.com
who went on to serve as Democratic senator from Ohio from 1974 to 1999.
"It seems more like two weeks barbour coatsthan 50 years," he said of the flight, noting it had since been "a rare day" when someone had not asked him a question about space or his flight.
The anniversary has been marked by a series of celebratory events, and Glenn has taken the opportunity to speak out againsthttp://www.barbourjacketsshop.com funding cuts to the nation's space program.
Glenn returned to space in 1998, at age 77, on board the space shuttle Discovery as a research subject for experiments on aging sponsored by the National Institutes of Health.