Drupal questions - how to print a form from another module?

I am trying to include a module form in a panel, and I tried to use drupal_get_form (), but not sure if I am using it correctly.

The organic groups module has a function for displaying og_broadcast_form. He called inside 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 function:

 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 tried several options for 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?

+3
source share
4 answers

FYI .. Your problem is that you are trying to load a form located in other modules, including a file. The function is in og.pages.inc, and you need to call:

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

, .

+4

drupal_get_form , form_id, , $form.

3 $args = func_get_args();, drupal_get_form , .

drupal_get_form('og_broadcast_form', node_load(arg(1)));.

, print, return? , . drupal_get_form , , .

EDIT: node, nid, % node , node_load (arg (1)) .

+2

drupal_get_form gets the form for you. Have you tried typing drupal_render (drupal_get_form ('whatever')))?

+1
source

drupal_get_form() accepts only one argument, $ form_id.

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

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

0
source

Source: https://habr.com/ru/post/1755628/


All Articles