Community Documentation

drupal_get_form

5 form.inc drupal_get_form($form_id)
6 form.inc drupal_get_form($form_id)
7 form.inc drupal_get_form($form_id)
8 form.inc drupal_get_form($form_id)

Wrapper for drupal_build_form() for use when $form_state is not needed.

Parameters

$form_id: The unique string identifying the desired form. If a function with that name exists, it is called to build the form array. Modules that need to generate the same form (or very similar forms) using different $form_ids can implement hook_forms(), which maps different $form_id values to the proper form constructor function. Examples may be found in node_forms(), search_forms(), and user_forms().

...: Any additional arguments are passed on to the functions called by drupal_get_form(), including the unique form constructor function. For example, the node_edit form requires that a node object is passed in here when it is called. These are available to implementations of hook_form_alter() and hook_form_FORM_ID_alter() as the array $form_state['build_info']['args'].

Return value

The form array.

See also

drupal_build_form()

Related topics

▾ 45 functions call drupal_get_form()

block_admin_display in modules/block/block.admin.inc
Menu callback for admin/structure/block.
book_outline in modules/book/book.pages.inc
Menu callback; show the outline form for a single node.
comment_admin in modules/comment/comment.admin.inc
Menu callback; present an administrative comment listing.
comment_confirm_delete_page in modules/comment/comment.admin.inc
Page callback for comment deletions.
comment_edit_page in modules/comment/comment.module
Page callback for comment editing.
comment_node_page_additions in modules/comment/comment.module
Build the comment-related elements for node detail pages.
comment_reply in modules/comment/comment.pages.inc
This function is responsible for generating a comment reply form. There are several cases that have to be handled, including:
dashboard_admin_blocks in modules/dashboard/dashboard.module
Menu page callback: builds the page for administering dashboard blocks.
dblog_overview in modules/dblog/dblog.admin.inc
Menu callback; displays a listing of log messages.
example_profile_tasks in developer/example.profile
Perform any final installation tasks for this profile.
field_test_entity_add in modules/field/tests/field_test.entity.inc
Menu callback: displays the 'Add new test_entity' form.
field_test_entity_edit in modules/field/tests/field_test.entity.inc
Menu callback: displays the 'Edit exiisting test_entity' form.
filter_admin_format_page in modules/filter/filter.admin.inc
Menu callback; Display a text format form.
FormCheckboxTestCase::testFormCheckbox in modules/simpletest/tests/form.test
form_test_two_instances in modules/simpletest/tests/form_test.module
Menu callback that returns two instances of the node form.
forum_form_main in modules/forum/forum.admin.inc
@file Administrative page callbacks for the forum module.
install_select_locale in includes/install.core.inc
Installation task; select which locale to use for the current profile.
install_select_profile in includes/install.core.inc
Installation task; select which profile to install.
ListFieldTestCase::testUpdateAllowedValues in modules/field/modules/list/tests/list.test
Test that allowed values can be updated.
locale_languages_add_screen in modules/locale/locale.admin.inc
User interface for the language addition screen.
locale_translate_delete_page in modules/locale/locale.admin.inc
String deletion confirmation page.
locale_translate_export_screen in modules/locale/locale.admin.inc
User interface for the translation export screen.
locale_translate_seek_screen in modules/locale/locale.admin.inc
String search screen.
menu_delete_menu_page in modules/menu/menu.admin.inc
Menu callback; check access and get a confirm form for deletion of a custom menu.
menu_item_delete_page in modules/menu/menu.admin.inc
Menu callback; Check access and present a confirm form for deleting a menu link.
node_add in modules/node/node.pages.inc
Returns a node submission form.
node_page_edit in modules/node/node.pages.inc
Menu callback; presents the node editing form.
openid_redirect in modules/openid/openid.inc
Creates a js auto-submit redirect for (for the 2.x protocol)
openid_user_identities in modules/openid/openid.pages.inc
Menu callback; Manage OpenID identities for the specified user.
path_admin_edit in modules/path/path.admin.inc
Menu callback; handles pages for creating and editing URL aliases.
path_admin_overview in modules/path/path.admin.inc
Return a listing of all defined URL aliases.
poll_block_latest_poll_view in modules/poll/poll.module
Return content for 'latest poll' block.
poll_view in modules/poll/poll.module
Implements hook_view().
search_block_view in modules/search/search.module
Implements hook_block_view().
search_embedded_form_preprocess_search_result in modules/search/tests/search_embedded_form.module
Adds the test form to search results.
search_view in modules/search/search.pages.inc
Menu callback; presents the search form and/or search results.
system_actions_manage in modules/system/system.admin.inc
Menu callback; Displays an overview of available and configured actions.
system_ip_blocking in modules/system/system.admin.inc
Menu callback. Display blocked IP addresses.
system_themes_page in modules/system/system.admin.inc
Menu callback; displays a listing of all themes.
template_preprocess_poll_results in modules/poll/poll.module
Preprocess the poll_results theme hook.
trigger_assign in modules/trigger/trigger.admin.inc
Builds the form that allows users to assign actions to triggers.
update_selection_page in ./update.php
user_admin in modules/user/user.admin.inc
user_block_view in modules/user/user.module
Implements hook_block_view().
user_page in modules/user/user.pages.inc
Access callback for path /user.

File

includes/form.inc, line 115

Code

<?php
function drupal_get_form($form_id) {
  $form_state = array();

  $args = func_get_args();
  // Remove $form_id from the arguments.
  array_shift($args);
  $form_state['build_info']['args'] = $args;

  return drupal_build_form($form_id, $form_state);
}
?>

Comments

In drupal 7 drupal_get_form()

In drupal 7 drupal_get_form() return Form API array for HTML output drupal_render() to be called. See http://drupal.org/node/224333#unrendered

Thanks for your comment!

Thanks for your comment!

$form_id can not start with

$form_id can not start with an underscore. My function was called something like _mymodule_form. The form was rendered but the submit function was not called. Renaming it to mymodule_form, it works fine.

additional arguments

It is possible to call drupal_get_form('some_form', $arg1, $arg2);
which will pass the arguments $arg1 and $arg2 (or however many arguments you pass) to the function some_form() like this:

some_form($form, &$form_state, $arg1, $arg2);

the function that does the work for this is drupal_retrieve_form()

agreed

agreed

drupal_get_form for node forms

Embedding a node creation form has gotten more complex as you need to use form_load_include. If you don't do this (and only include node.pages.inc via module_load_include) ajax on 'add another' fields will break with 500 errors.

<?php
global $user;
$node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'FOO', 'language' => LANGUAGE_NONE);
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
return
drupal_build_form('FOO_node_form', $form_state);
?>

... and forms for other entity types

Thanks, that saved me a lot of screaming at the monitor. I was getting quite hoarse. In my case, the problem was with embedding the form for another entity type in a page, but the same solution applies.

Passing arguments to drupal_get_form()

<?php
function mymodule_menu(){
  ...

 
$items['abc'] = array(
   
'type' => MENU_CALLBACK,
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('add_component_form'),
   
'access arguments' => array('access content')
  );

  return
$items;
}

function
add_component_form($form, &$form_state){
 
$arg1 = $form_state['build_info']['args'][0];
  ...
}
?>

When path 'abc/def' is requested, the $arg1 will be 'def'.

Login or register to post comments