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.

Comments

coreyp_1’s picture

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

- Corey

evoltech’s picture

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

coreyp_1’s picture

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

jmlavarenne’s picture

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)."