Hey guys,
I'm trying to do something that should be extremely easy but after a few hours of pouring through the form api documentation, I am not having any luck.
Basically all I want to do is pass a value into my form and use it as a value of a hidden field. See this example:
//I create form using drupal_get_form()
drupal_get_form('myform')
//In myform I have something like
form['hidden_value'] = array(
'#type' = hidden,
'#value' = $passed_variable,
)
So the question is how do I actually get $passed_variable into the form function? I can't see anything in drupal_get_form that would allow me to pass a variable through and all the other options seem to be for pre-processing or validation.
Please help!
Comments
Pass it along in drupal_get_form()
If I understand correctly, then you just need to pass it through on your drupal_get_form call:
Thanks for the
Thanks for the response!
When I try to pass in a variable through the drupal_get_form function in the manner you showed, I get the following error:
Basically from what I can tell, the function isn't expecting my varialbe as the 2nd parameter of the function and trigers an error. Not only that but the variable doesn't get passed into the actual form function anyway.
There must be a way to do this though as setting hidden element values is a very common and standard thing. Anyway, if I'm doing something wrong or if anybody else knows how to get a variable into the form function, let me know.
Thanks,
Chris
Ok, I figured it out. It was
Ok, I figured it out. It was an issue with differences in the API between 5 and 6. I am going to post an example so that anyone searching in the future doesn't have to waist a lot of time like I did.
Pass Variable Into Form 6.x:
Hope this helps someone down the line.
Well done on passed by
Well done on passed by variable but i still have a problem on $passed_variable. myModule_form($form_id, $passed_variable) is always return Array type. Can anyone explain to me why it turn to Array in that way?
Thanks
Thank you, this saved me a lot of hassle it seems.
Yes, the first variable in a form function needs to be the $form_state variable.
In my case the second variable is an array populating the select option list and your approach works fine.
Thanks, after a few hours
Thanks, after a few hours your post gave me the answer I was looking for.
Cheers,
Chris
For anyone arriving at this
For anyone arriving at this looking for a Drupal 7 answer, the answer (at least in my case) was:
Otherwise you'll get the array to string conversion error messages.
Ian Bullock
content type form
One of the things that has greatly tripped me up ... is if you are following the module development guides, and creating your content type and define your form like so
This is used for add/edit form of the default "Create content" for your module.
This is not the same as the hook_form examples in the FAPI where you are defining
The first goes through some automagical intermediate steps and drupalfied in some manner. In that one, you will not receive any additional arguments from drupal_get_form() ... instead you must use the node object you are preparing as an intermediary.
Then from the signature in the first code block, you can access $node->passed