I have a module that displays a form on certain pages. The object is to use this form to add some data to the node that is being displayed on the same page. (I know, why not just add the fields to the Edit form--but that's not what the boss wants.) I am using hook_nodeapi to add the form to the node like so:

function my_module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->type == 'this_type') {
        $node->content['add_my_form'] = array(
                '#value' => drupal_get_form('my_form'),
                '#weight' => 50,
              );
      }
      break;
  }
}

My problem is that I can't figure out how to pass the nid to the form, either at the construction stage, or whenever, so long as I can use it when the form is submitted to put the data where it belongs.

Any help appreciated. Am I even going about this in the right way?

Comments

baldwinlouie’s picture

You can pass arguments into the drupal_get_form() function. So what you can do is pass the nid there like such

        if ($node->type == 'this_type') {
        $node->content['add_my_form'] = array(
                '#value' => drupal_get_form('my_form', $node->nid),
                '#weight' => 50,
              );
      }

Then your form function looks like such

function my_form($nid) {
}

Then you will have access to the nid inside the form.

Baldwin Louie,
BitSprout LLC
http://www.bitsprout.net

zbricoleur’s picture

Totally works. And so easy...once you know how ;-)