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