| 7 form.inc | drupal_get_form($form_id) |
| 4.7 form.inc | drupal_get_form($form_id, & |
| 5 form.inc | drupal_get_form($form_id) |
| 6 form.inc | drupal_get_form( |
| 8 form.inc | drupal_get_form($form_arg) |
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
Related topics
- authorize.php in ./
authorize.php - Administrative script for running authorized file operations.
- 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: Shows 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.
- aggregator_menu in modules/
aggregator/ aggregator.module - Implements hook_menu().
- ajax_forms_test_menu in modules/
simpletest/ tests/ ajax_forms_test.module - Implements hook_menu().
- batch_test_menu in modules/
simpletest/ tests/ batch_test.module - Implement hook_menu().
- block_menu in modules/
block/ block.module - Implements hook_menu().
- book_menu in modules/
book/ book.module - Implements hook_menu().
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()
PermalinkIn drupal 7
drupal_get_form()return Form API array for HTML outputdrupal_render()to be called. See http://drupal.org/node/224333#unrendered$form_id can not start with
Permalink$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
PermalinkIt 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()It is possible
PermalinkYes,that is possible,I tried in D7 and it works!!! :)
drupal_get_form for node forms
PermalinkEmbedding 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.
<?phpglobal $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
PermalinkThanks, 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
PermalinkI am grateful to you for this information.
at the risk of repeating
Permalinkat the risk of repeating what's already been said... THANK YOU for that hint about form_load_include ! saved my sorry ass, that did!
That saved me a day's work!
PermalinkI registered a custom route and was trying to show a node form like following:
<?php
global $user;
module_load_include('inc', 'node', 'node.pages');
$node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'foo', 'language' => LANGUAGE_NONE);
return drupal_get_form('foo_node_form', $node);
?>
and my file upload widget was not working. As soon as I was trying to upload a file, the upload widget itself was disappearing!
Then I google and found your solution which worked like magic! Thanks so much! it would be really great if there was a quick explaination! The code works but I didn't really learn much! Thanks in advance!
thank you very very much :)
Permalinkthank you very very much :)
i spend all day to get things works with my drupal_get_form function.
Finally with your code it worked.
Maybe it's a bug
PermalinkMaybe it's a bug that you can't do this with a simple node_add('NODETYPE') call when ajax is in the game. This is a huge pain for those who get stuck on this.
Anyway, thank you very much for this snippet.
Passing arguments to drupal_get_form()
Permalink<?phpfunction 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'.
Is 'page arguments' array can take two forms function?
PermalinkI have used this code and it does not work for me:-
function test_menu(){
$items = array();
$items['test'] = array(
'title' => 'Form validation with ajax',
'description' => 'testing form validation and ajax dropdown with ajax',
'access callback' => 'user_access',
'access arguments' => array( 'validate ajax form' ),
'page callback' => 'drupal_get_form',
'page arguments'=> array( 'validate_ajax_form', 'product_form' )
);
return $items;
}
/*
* implements hook_form
*/
function validate_ajax_form( $form, &$form_state ){
$product_groups = array('' => 'Select product group');
$form['product_group_id'] = array(
'#type' => 'select',
'#options' => $product_groups
);
return $form;
}
function product_form( $form, &$form_state ){
$form['name'] = array(
'#type' => 'textfield',
'#default_value' => $name
);
return $form;
}
But it does not print two forms individually.