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)

Returns a renderable form array for a given form ID.

This function should be used instead of drupal_build_form() when $form_state is not needed (i.e., when initially rendering the form) and is often used as a menu callback.

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(), and search_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 calls to drupal_get_form()

32 string references to 'drupal_get_form'

File

includes/form.inc, line 123
Functions for form and batch generation and processing.

Code

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.

I am grateful to you for this

I am grateful to you for this information.

at the risk of repeating

at the risk of repeating what's already been said... THANK YOU for that hint about form_load_include ! saved my sorry ass, that did!

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

...well this is interesting.

+1

Get page/form arguments from URL

<?php
function MYMODULE_menu(){

 
$items['object/%'] = array(
   
'title' => 'Object',
   
'access callback' => TRUE,
   
'page callback' => 'object_page',
   
'page arguments' => array(1),
   
'type' => MENU_NORMAL_ITEM,
  );

 
$items['object/%/general'] = array(
   
'title' => 'General Info',
   
'access callback' => TRUE,
   
'type' => MENU_DEFAULT_LOCAL_TASK,
  );

 
$items['object/%/history'] = array(
   
'title' => 'History',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('history_form'),
   
'access callback' => TRUE,
   
'type' => MENU_LOCAL_TASK,
  );

  ...
}

function
object_page($arg1){
  ...
}

function
history_form($form, &$form_state){
 
$arg1 = arg(1);
}
?>

Login or register to post comments