Drupal - Passing arguments to drupal_get_form()

Just add $vars as second argument.

drupal_get_form('new_form', $vars);

and...

function new_form($form, &$form_state, $vars) {
// ...

Quote from drupal_get_form()

... 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 is passed in here when it is called. These are available to implementations of hook_form_alter() and hook_form_FORM_ID_alter() as the array $form_state['build_info']['args'].


The additional arguments you pass to drupal_get_form() are available in $form_state['build_info']['args'], but you can't have function calls in your page arguments. I'd suggest the following approach:

function mymodule_menu() {
    $items = array();
    $items['mymodule/example'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('mymodule_form'),
    );
    return $items;
}


function mymodule_form($form, &$form_state) {
  // this function now uses dev/user friendly named keys
  $vars = mymodule_example_function();
  $form = array();

  $form['heading'] = array(
    '#markup' => check_plain($vars['heading']),
  );

  // use other arguments here

  return $form;
}

The rest of what you need is in the Form API reference

Tags:

Forms

7