embedding a form in a node view

evoltech - July 4, 2008 - 06:18

I am writing a module for a custom content type and has built in use of the voting api with some custom voting criteria. When displaying the node I would like to include a few different forms that update the vote associated with that node instead of having the user go the special edit page. The idea is that a user might be able to look at a view of these content types and just go down the list clicking on buttons that cast votes. The problem is that I can't seem to figure out how to get the node id in the form. This value is available when viewing the page by itself from arg(0), but not as a list. In my hook_view() I am adding the form with some code like this:

$node->content['node_form'] = array( '#value' => drupal_get_form('form_name') );

I haven't found anyway to reference the node that was associated with this form so that I can execute the appropriate sql in my hook_submit function.

Any help is appreciated.

Why not just pass it as a

coreyp_1 - July 4, 2008 - 06:36

Why not just pass it as a hidden element in the form?

- Corey

I tried that and...

evoltech - July 4, 2008 - 06:42

That is what I would like to do, but drupal_get_form() only accepts one argument; the form id. It then calls the function by the form id (in the example above this would be form_name(). In that function the only way I have been able to get access to the node id is through arg(0), which only works in a single node view.

hackbloc.org : exploit code not people

Warning: This is an ugly

coreyp_1 - July 4, 2008 - 07:02

Warning: This is an ugly hack, but it should work...

OK, just set a global variable, say $_current_node_id, with the node id in question, then, in the form-building function, check out the variable to see what id to use in the hidden element.

Yes, it's ugly, and, yes, it's breaking coding convention rules by using a global variable to pass variables to a function, but "ya gotta' do what ya gotta do!"

- Corey

... Any additional arguments needed ...

Blackguard - May 12, 2009 - 20:20

You can pass extra arguments to drupal_get_form() as indicated in the API http://api.drupal.org/api/function/drupal_get_form

In Drupal 6 ''...it should be mentioned that the first argument of the function to be called by drupal_get_form must be &$form_state (or whatever you like it to call)."

 
 

Drupal is a registered trademark of Dries Buytaert.