Community Documentation

hook_form_alter

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()

hook_form_FORM_ID_alter()

Related topics

▾ 44 functions implement hook_form_alter()

aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
block_form_form_test_alter_form_alter in modules/simpletest/tests/form_test.module
Implements hook_form_FORM_ID_alter() on behalf of block.module.
block_form_user_profile_form_alter in modules/block/block.module
Implements hook_form_FORM_ID_alter() for user_profile_form().
book_form_node_form_alter in modules/book/book.module
Implements hook_form_BASE_FORM_ID_alter().
comment_form_node_form_alter in modules/comment/comment.module
Implements hook_form_BASE_FORM_ID_alter().
comment_form_node_type_form_alter in modules/comment/comment.module
Implements hook_form_FORM_ID_alter().
contact_form_user_profile_form_alter in modules/contact/contact.module
Implements hook_form_FORM_ID_alter().
dashboard_form_block_add_block_form_alter in modules/dashboard/dashboard.module
Implements hook_form_FORM_ID_alter().
dashboard_form_block_admin_display_form_alter in modules/dashboard/dashboard.module
Implements hook_form_FORM_ID_alter().
dashboard_form_dashboard_admin_display_form_alter in modules/dashboard/dashboard.module
Implements hook_form_FORM_ID_alter().
example_form_alter in developer/example.profile
Implementation of hook_form_alter().
field_test_field_widget_form_alter in modules/field/tests/field_test.module
Implements hook_field_widget_form_alter().
field_ui_form_node_type_form_alter in modules/field_ui/field_ui.module
Implements hook_form_FORM_ID_alter().
form_test_form_alter in modules/simpletest/tests/form_test.module
Implements hook_form_alter().
form_test_form_form_test_alter_form_alter in modules/simpletest/tests/form_test.module
Implements hook_form_FORM_ID_alter().
form_test_form_user_register_form_alter in modules/simpletest/tests/form_test.module
Implements hook_form_FORM_ID_alter() for the registration form.
forum_form_alter in modules/forum/forum.module
Implements hook_form_alter().
hook_field_widget_form_alter in modules/field/field.api.php
Alter forms for field widgets provided by other modules.
hook_field_widget_WIDGET_TYPE_form_alter in modules/field/field.api.php
Alter widget forms for a specific widget provided by another module.
locale_form_alter in modules/locale/locale.module
Implements hook_form_alter().
locale_form_comment_form_alter in modules/locale/locale.module
Implements hook_form_FORM_ID_alter().
locale_form_node_form_alter in modules/locale/locale.module
Implements hook_form_BASE_FORM_ID_alter().
locale_form_node_type_form_alter in modules/locale/locale.module
Implements hook_form_FORM_ID_alter().
locale_form_path_admin_form_alter in modules/locale/locale.module
Implements hook_form_FORM_ID_alter().
menu_form_node_form_alter in modules/menu/menu.module
Implements hook_form_BASE_FORM_ID_alter().
menu_form_node_type_form_alter in modules/menu/menu.module
Implements hook_form_FORM_ID_alter().
minimal_form_install_configure_form_alter in profiles/minimal/minimal.profile
Implements hook_form_FORM_ID_alter().
node_access_test_form_node_form_alter in modules/node/tests/node_access_test.module
Implements hook_form_BASE_FORM_ID_alter().
node_form_block_add_block_form_alter in modules/node/node.module
Implements hook_form_FORMID_alter().
node_form_search_form_alter in modules/node/node.module
Implements hook_form_FORM_ID_alter().
openid_form_user_register_form_alter in modules/openid/openid.module
Implements hook_form_FORM_ID_alter().
overlay_form_user_profile_form_alter in modules/overlay/overlay.module
Implements hook_form_FORM_ID_alter().
path_form_node_form_alter in modules/path/path.module
Implements hook_form_BASE_FORM_ID_alter().
profile_form_alter in modules/profile/profile.module
Implements hook_form_alter().
standard_form_install_configure_form_alter in profiles/standard/standard.profile
Implements hook_form_FORM_ID_alter().
system_form_form_test_alter_form_alter in modules/simpletest/tests/form_test.module
Implements hook_form_FORM_ID_alter() on behalf of system.module.
system_form_user_profile_form_alter in modules/system/system.module
Implements hook_form_FORM_ID_alter().
system_form_user_register_form_alter in modules/system/system.module
Implements hook_form_FORM_ID_alter().
taxonomy_test_form_alter in modules/simpletest/tests/taxonomy_test.module
Implements hook_form_alter().
translation_form_node_form_alter in modules/translation/translation.module
Implements hook_form_BASE_FORM_ID_alter().
translation_form_node_type_form_alter in modules/translation/translation.module
Implements hook_form_FORM_ID_alter().
user_form_field_ui_field_edit_form_alter in modules/user/user.module
Implements hook_form_FORM_ID_alter().
_color_theme_select_form_alter in modules/color/color.module
Helper for hook_form_FORM_ID_alter() implementations.
_openid_user_login_form_alter in modules/openid/openid.module

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')),
    );
  }
}
?>

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:

<?php
function 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...):

<?php
function 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:

<?php
function 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.

Login or register to post comments