drupal_prepare_form

Versions
5
drupal_prepare_form($form_id, &$form)
6 – 7
drupal_prepare_form($form_id, &$form, &$form_state)

Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering.

Parameters

$form_id A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.

$form An associative array containing the structure of the form.

$form_state A keyed array containing the current state of the form. Passed in here so that hook_form_alter() calls can use it, as well.

Related topics

▾ 4 functions call drupal_prepare_form()

drupal_build_form in includes/form.inc
Build and process a form based on a form id.
drupal_form_submit in includes/form.inc
Retrieves a form using a form_id, populates it with $form_state['values'], processes it, and returns any validation errors encountered. This function is the programmatic counterpart to drupal_get_form().
drupal_rebuild_form in includes/form.inc
Retrieves a form, caches it and processes it with an empty $_POST.
openid_authentication in modules/openid/openid.module
Authenticate a user or attempt registration.

Code

includes/form.inc, line 637

<?php
function drupal_prepare_form($form_id, &$form, &$form_state) {
  global $user;

  $form['#type'] = 'form';
  $form_state['programmed'] = isset($form_state['programmed']) ? $form_state['programmed'] : FALSE;

  if (isset($form['#build_id'])) {
    $form['form_build_id'] = array(
      '#type' => 'hidden',
      '#value' => $form['#build_id'],
      '#id' => $form['#build_id'],
      '#name' => 'form_build_id',
    );
  }

  // Add a token, based on either #token or form_id, to any form displayed to
  // authenticated users. This ensures that any submitted form was actually
  // requested previously by the user and protects against cross site request
  // forgeries.
  if (isset($form['#token'])) {
    if ($form['#token'] === FALSE || $user->uid == 0 || $form_state['programmed']) {
      unset($form['#token']);
    }
    else {
      $form['form_token'] = array('#type' => 'token', '#default_value' => drupal_get_token($form['#token']));
    }
  }
  elseif (isset($user->uid) && $user->uid && !$form_state['programmed']) {
    $form['#token'] = $form_id;
    $form['form_token'] = array(
      '#id' => drupal_html_id('edit-' . $form_id . '-form-token'),
      '#type' => 'token',
      '#default_value' => drupal_get_token($form['#token']),
    );
  }

  if (isset($form_id)) {
    $form['form_id'] = array(
      '#type' => 'hidden',
      '#value' => $form_id,
      '#id' => drupal_html_id("edit-$form_id"),
    );
  }
  if (!isset($form['#id'])) {
    $form['#id'] = drupal_html_id($form_id);
  }

  $form += element_info('form');
  $form += array('#tree' => FALSE, '#parents' => array());

  if (!isset($form['#validate'])) {
    if (function_exists($form_id . '_validate')) {
      $form['#validate'] = array($form_id . '_validate');
    }
  }

  if (!isset($form['#submit'])) {
    if (function_exists($form_id . '_submit')) {
      // We set submit here so that it can be altered.
      $form['#submit'] = array($form_id . '_submit');
    }
  }

  // Invoke hook_form_FORM_ID_alter() implementations.
  drupal_alter('form_' . $form_id, $form, $form_state);

  // Invoke hook_form_alter() implementations.
  drupal_alter('form', $form, $form_state, $form_id);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.