[development] $form_state passed to form function from drupal_get_form()

Steve Edwards killshot91 at comcast.net
Wed Dec 9 01:05:02 UTC 2009


I have a form that I would like to vary slightly depending on the 
context in which is it being used, changing things such as the form 
submit button text and the submit function.  To indicate the context, I 
simply need to pass a string to the form function.  According to 
http://api.drupal.org/api/function/drupal_get_form/6, "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 be passed in here when it is 
called."

So, I define my variable and call my function:

$create = 'create';
$output .= drupal_get_form('my_form_function', $create);

As indicated in the text from the page (mentioned above), the node_add() 
function does something similar:

$output = drupal_get_form($type .'_node_form', $node);

Within my form function, I assign the button value with a case statement 
based on that.  However, when I run the function, I get no value for my 
button, and a breakpoint shows that there is no value for my variable.  
Stepping through some more, I find that when it gets to my form 
function, my parameter that I passed from my call to drupal_get_form is 
actually the second one, and there is a skeleton $form_state array 
passed as the first parameter.  When I add $form_state as the first 
parameter in my form and move $created to the second one, it works 
fine.  I can see in line 371 of form.inc where the form function is 
called using

$form = call_user_func_array(isset($callback) ? $callback : $form_id, 
$args);

and at that point  $args[0] is basically $form_state and $args[1] is my 
parameter that is passed ($created in this case).   My question is, why 
this seeming discrepancy between what the docs indicate and what is 
actually passed to my form function?  I haven't seen anything written in 
PDD or online that indicates that I have to have $form_state as the 
first parameter in my form function definition when I want to pass it a 
variable.  Am I missing something?

Thanks.

Steve


More information about the development mailing list