drupal_get_form

Definition

drupal_get_form($form_id, &$form, $callback = NULL)
includes/form.inc, line 60

Description

Processes a form array and produces the HTML output of a form. If there is input in the $_POST['edit'] variable, this function will attempt to validate it, using drupal_validate_form(), and then submit the form using drupal_submit_form().

Parameters

$form_id A unique string identifying the form. Allows each form to be themed. Pass NULL to suppress the form_id parameter (produces a shorter URL with method=get)

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

$callback An optional callback that will be used in addition to the form_id.

Related topics

Namesort iconDescription
Form generationFunctions to enable output of HTML forms and form elements.

Code

<?php
function drupal_get_form($form_id, &$form, $callback = NULL) {
  global $form_values, $form_submitted, $user, $form_button_counter;
  static $saved_globals = array();

  // Save globals in case of indirect recursive call
  array_push($saved_globals, array($form_values, $form_submitted, $form_button_counter));

  $form_values = array();
  $form_submitted = FALSE;
  $form_button_counter = array(0, 0);

  $form['#type'] = 'form';

  // Add a token to any form displayed to authenticated users.
  // This ensures that any submitted form was actually requested previously by the user to protect against
  // cross site request forgeries. The default token can be bypassed by setting $form['#token'] to FALSE.

  if (isset($form['#token'])) {
    if ($form['#token'] === FALSE || $user->uid == 0) {
      unset($form['#token']);
    }
    else {
      $form['form_token'] = array('#type' => 'token', '#default_value' => drupal_get_token($form['#token']));
    }
  }
  else if ($user->uid) {
    $form['#token'] = $form_id;
    $form['form_token'] = array(
      '#id' => 'edit-'. str_replace('_', '-', $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' => str_replace('_', '-', "edit-$form_id"));
  }
  if (!isset($form['#id'])) {
    $form['#id'] = $form_id;
  }

  $form += _element_info('form');

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

  if (!isset($form['#submit'])) {
    if (function_exists($form_id .'_submit')) {
      // we set submit here so that it can be altered but use reference for
      // $form_values because it will change later
      $form['#submit'] = array($form_id .'_submit' => array());
    }
    elseif (function_exists($callback .'_submit')) {
      $form['#submit'] = array($callback .'_submit' => array());
    }
  }

  foreach (module_implements('form_alter') as $module) {
    $function = $module .'_form_alter';
    $function($form_id, $form);
  }

  $form = form_builder($form_id, $form);
  if (!empty($_POST['edit']) && (($_POST['edit']['form_id'] == $form_id) || ($_POST['edit']['form_id'] == $callback))) {
    drupal_validate_form($form_id, $form, $callback);
    // IE does not send a button value when there is only one submit button (and no non-submit buttons)
    // and you submit by pressing enter.
    // In that case we accept a submission without button values.
    if (($form_submitted || (!$form_button_counter[0] && $form_button_counter[1])) && !form_get_errors()) {
      $redirect = drupal_submit_form($form_id, $form, $callback);
      if (isset($redirect)) {
        $goto = $redirect;
      }
      if (isset($form['#redirect'])) {
        $goto = $form['#redirect'];
      }
      if ($goto !== FALSE) {
        if (is_array($goto)) {
          call_user_func_array('drupal_goto', $goto);
        }
        elseif (!isset($goto)) {
          drupal_goto($_GET['q']);
        }
        else {
          drupal_goto($goto);
        }
      }
    }
  }

  // Don't override #theme if someone already set it.
  if (!isset($form['#theme'])) {
    if (theme_get_function($form_id)) {
      $form['#theme'] = $form_id;
    }
    elseif (theme_get_function($callback)) {
      $form['#theme'] = $callback;
    }
  }

  if (isset($form['#pre_render'])) {
    foreach ($form['#pre_render'] as $function) {
      if (function_exists($function)) {
        $function($form_id, $form);
      }
    }
  }

  $output = form_render($form);
  // Restore globals
  list($form_values, $form_submitted, $form_button_counter) = array_pop($saved_globals);
  return $output;
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.