hook_form_alter

  1. drupal
    1. 5
    2. 8 system.api.php
    3. 6
    4. 7 system.api.php
Versions
5 hook_form_alter($form_id, &$form)
6 – 8 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 retrieved at 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

Code

documentation/developer/hooks/core.php, line 744

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