views:

82

answers:

4

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.

In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:

    // Broadcast tab on group node.
  $items['node/%node/broadcast'] = array(
    'title' => 'Broadcast',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('og_broadcast_form', 1),
    'access callback' => 'og_broadcast_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'file' => 'og.pages.inc',
    'weight' => 7
  );

And in og.pages.inc, the function is:

 function og_broadcast_form($form_state, $node) {
   drupal_set_title(t('Send message to %group', array('%group' => $node->title)));

   if (!empty($form_state['post'])) {
     drupal_set_message(t('Your message will be sent to all members of this group.'));
   }

   $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#size' => 70,
    '#maxlength' => 250,
    '#description' => t('Enter a subject for your message.'),
    '#required' => TRUE,
  );
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#rows' => 5,
    '#cols' => 90,
    '#description' => t('Enter a body for your message.'),
    '#required' => TRUE
  );
  $form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
  $form['gid'] = array('#type' => 'value', '#value' => $node->nid);
  return $form;
}

I've tried a number of variations of drupal_get_form:

print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n); 

etc., etc... Is there a way to accomplish what I'm trying to do here?

A: 

drupal_get_form() only accepts one argument, the $form_id.

http://api.drupal.org/api/function/drupal_get_form/6

Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.

Kevin
Thanks, but I tried that as well... It's the second one I listed and it didn't render the form at all?
Dashiell0415
That's not correct; `drupal_get_form()` does accept more than one argument. See the description for the parameters, which reports "... 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.", right after the description of the parameter `$form_id`.
kiamlaluno
Why not make it part of the arg list then? Flew right by me.
Kevin
+2  A: 

If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.

On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.

You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.

Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.

EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.

Grayside
A: 

FYI.. your problem is that you're attempting to load a form located in another modules include file. The function is located in og.pages.inc and you'll need to make a call to:

module_load_include('inc', 'og', 'og.pages');

This must be done before you can grab the form.

James G
A: 

drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?

barraponto