drupal_get_form

Versions
4.7
drupal_get_form($form_id, &$form, $callback = NULL)
5 – 7
drupal_get_form($form_id)

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

▾ 56 functions call drupal_get_form()

aggregator_form_feed in modules/aggregator.module
Generate a form to add/edit feed sources.
archive_browse_form in modules/archive.module
Generate a form that retrieves archives for a certain date.
block_admin_configure in modules/block.module
Menu callback; displays the block configuration form.
block_admin_display in modules/block.module
Generate main block administration form.
block_box_add in modules/block.module
Menu callback; displays the block creation form.
book_admin_edit in modules/book.module
Display an administrative view of the hierarchy of a book.
book_admin_orphan in modules/book.module
Menu callback; displays a listing of all orphaned book pages.
book_outline in modules/book.module
Implementation of function book_outline() Handles all book outline operations.
comment_admin_overview in modules/comment.module
Menu callback; present an administrative comment listing.
comment_controls in modules/comment.module
comment_form in modules/comment.module
confirm_form in modules/system.module
contact_admin_edit in modules/contact.module
Category edit page.
contact_admin_settings in modules/contact.module
Settings tab. Using a form rather than hook_settings().
contact_mail_page in modules/contact.module
Site-wide contact page
contact_mail_user in modules/contact.module
Personal contact page.
filter_admin_format_form in modules/filter.module
Generate a filter format form.
filter_admin_order in modules/filter.module
Menu callback; display form for ordering filters for a format.
filter_admin_overview in modules/filter.module
Displays a list of all input formats and which one is the default
forum_form_container in modules/forum.module
Returns a form for adding a container to the forum vocabulary
forum_form_forum in modules/forum.module
Returns a form for adding a forum to the forum vocabulary
menu_edit_item_form in modules/menu.module
Present the menu item editing form.
menu_edit_menu_form in modules/menu.module
Menu callback; handle the adding/editing of a new menu.
node_admin_nodes in modules/node.module
Menu callback: content administration.
node_filter_form in modules/node.module
Return form for node administration filters.
node_form in modules/node.module
Generate the node editing form.
path_form in modules/path.module
Return a form for editing or creating an individual URL alias.
poll_view_voting in modules/poll.module
Generates the voting form for a poll.
profile_field_form in modules/profile.module
Menu callback: Generate a form to add/edit a user profile field.
search_box in modules/search.module
Output a search form for the search block and the theme's search box.
search_form in modules/search.module
Render a search form.
system_modules in modules/system.module
Menu callback; displays a listing of all modules.
system_settings_form in modules/system.module
system_themes in modules/system.module
Menu callback; displays a listing of all themes.
taxonomy_form_term in modules/taxonomy.module
taxonomy_form_vocabulary in modules/taxonomy.module
Display form for adding and editing vocabularies.
update_selection_page in ./update.php
user_admin_access_add in modules/user.module
Menu callback: add an access rule
user_admin_access_check in modules/user.module
Menu callback: check an access rule
user_admin_access_edit in modules/user.module
Menu callback: edit an access rule
user_admin_perm in modules/user.module
Menu callback: administer permissions.
user_admin_role in modules/user.module
Menu callback: administer roles.
user_block in modules/user.module
Implementation of hook_block().
user_edit in modules/user.module
user_login in modules/user.module
user_pass in modules/user.module
user_pass_reset in modules/user.module
Menu callback; process one time login link and redirects to the user page on success.
user_register in modules/user.module
watchdog_overview in modules/watchdog.module
Menu callback; displays a listing of log messages.
_aggregator_page_list in modules/aggregator.module
Prints an aggregator page listing a number of feed items. Various menu callbacks use this function to print their feeds.
_locale_admin_export_screen in includes/locale.inc
User interface for the translation export screen
_locale_admin_import_screen in includes/locale.inc
User interface for the translation import screen.
_locale_admin_manage_add_screen in includes/locale.inc
User interface for the language addition screen.
_locale_admin_manage_screen in includes/locale.inc
User interface for the language management screen.
_locale_string_edit in includes/locale.inc
User interface for string editing.
_locale_string_seek_form in includes/locale.inc
User interface for the string search screen

Code

includes/form.inc, line 60

<?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;
}
?>
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.