function hook_form_alter

7.x system.api.php hook_form_alter(&$form, &$form_state, $form_id)
5.x core.php hook_form_alter($form_id, &$form)
6.x core.php hook_form_alter(&$form, &$form_state, $form_id)
8.x 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()

forms_api_reference.html

Related topics

42 functions implement hook_form_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_form_aggregator_admin_form_alter in drupal/modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
block_form_form_test_alter_form_alter in drupal/modules/simpletest/tests/form_test.module
Implements hook_form_FORM_ID_alter() on behalf of block.module.
block_form_user_profile_form_alter in drupal/modules/block/block.module
Implements hook_form_FORM_ID_alter() for user_profile_form().
book_form_node_form_alter in drupal/modules/book/book.module
Implements hook_form_BASE_FORM_ID_alter() for node_form().
comment_form_node_form_alter in drupal/modules/comment/comment.module
Implements hook_form_BASE_FORM_ID_alter().

... See full list

File

drupal/modules/system/system.api.php, line 1671
Hooks provided by Drupal core and the System module.

Code

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