function hook_form_alter

7.x system.api.php hook_form_alter(&$form, &$form_state, $form_id)
6.x core.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 retrieved from $form['#node'].

Note that instead of hook_form_alter(), which is called for all forms, you can also use hook_form_FORM_ID_alter() to alter a specific form.

Parameters

$form: Nested array of form elements that comprise the form. The arguments that drupal_get_form() was originally called with are available in the array $form['#parameters'].

$form_state: A keyed array containing the current state of the form.

$form_id: String representing the name of the form itself. Typically this is the name of the function that generated the form.

Related topics

15 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.

book_form_alter in drupal-6.x/modules/book/book.module
Implementation of hook_form_alter(). Adds the book fieldset to the node form.
color_form_alter in drupal-6.x/modules/color/color.module
Implementation of hook_form_alter().
comment_form_alter in drupal-6.x/modules/comment/comment.module
Implementation of hook_form_alter().
default_form_alter in drupal-6.x/profiles/default/default.profile
Implementation of hook_form_alter().
example_form_alter in documentation-6.x/developer/example.profile
Implementation of hook_form_alter().

... See full list

2 invocations of hook_form_alter()
drupal_prepare_form in drupal-6.x/includes/form.inc
Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering.
upload_js in drupal-6.x/modules/upload/upload.module
Menu-callback for JavaScript-based uploads.

File

documentation-6.x/developer/hooks/core.php, line 754
These are the hooks that are invoked by the Drupal core.

Code

function hook_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
    $form['path'] = array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($path),
      '#access' => user_access('create url aliases'),
      '#weight' => 30,
    );
    $form['path']['path'] = array(
      '#type' => 'textfield',
      '#default_value' => $path,
      '#maxlength' => 128,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Optionally specify an alternative URL by which this node can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
    );
    if ($path) {
      $form['path']['pid'] = array(
        '#type' => 'value',
        '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
      );
    }
  }
}