My form shall be used with preset values, such as

name=Johnny Winter,
street=66 Summerstreet
city=Autumnville 99999

I build my form with the forms.api and call it through
drupal_get_form(contact).

So for my example it went like this:

drupal_get_form(contact);

function contact() {

// Name
$form['contact']['name'] = array(
	'#type' => 'textfield',
	'#title' => t('Name'),
	'#value' => 'Johnny Winter',
	'#required' => TRUE,
);

// Street
$form['contact']['street'] = array(
	'#type' => 'textfield',
	'#title' => t('Street'),
	'#value' => '66 Summerstreet',
	'#required' => TRUE,
);

// City
$form['contact']['city'] = array(
	'#type' => 'textfield',
	'#title' => t('City'),
	'#value' => 'Autumnville 99999',
	'#required' => TRUE,
);

...
...
...
}

The best it would be I could pass an array with all form-fields like:
drupal_get_form(contact, $formfields);

If someone could help me with this, I would appreciate a lot!
Thank you,
Soezkan

Comments

modul’s picture

Hi Soezkan,

Very simple: in your function call, you can insert whatever parameters you want (well, I assume there is a limit, but for all practical purposes, there isn't. And then in the actual function, you can use these parameters just as you would in any other function: you can show them, make them editable, etc., and finally, you can toss them in the Submit function. For instance, I have a form which sends a bunch of mail messages. I construct the various parameters (subject, receivers, some interesting url etc.), and then call my function like this:

drupal_get_form('send_mymail', $subject, $toarray, $headers, $message, $namesarray, arg(2));

In the function, it goes like this:

function send_mymail($somesubj, $someto, $somehead, $somemessage, $somenames, $someurl) {
$form = array();
 // ......

$form['formmessage'] = array(
  '#type' => 'textfield',
  '#access' => false, 
  '#size' => 100,
  '#title' => 'message',
  '#description' => t('message'),
  '#value' => $somemessage, 
);
// So, for example, $somemessage is passed as the content of a form element
// ... all the way to the Submit butten :-)

soezkan’s picture

Thank you so much for the great help!

All the best,
Soezkan

perforator’s picture

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

$items['myfoo/%'] = array(
        ...
        'page callback' => 'drupal_get_form',
        'page arguments' => array('mymodule_myfoo',1),
        ...
        'type' => MENU_CALLBACK
                                      );
}

function mymodule_myfoo( &$form_state, $thearg ){
   $form = array();
   ....
   return $form;
}
modul’s picture

Not sure I'm following you here, perforator. Maybe that &$form_state thing is the case in Drupal 6.x? To my knowledge, it's at least Not the case in Drupal 5.x.

dropcube’s picture

Yes, only for 6.x.

cwebster’s picture

Thanks for this. That little clarification was really helpful.

sabeeln’s picture

thanx for this.. :)

Anonymous’s picture

Thank you for that perforator, I just spent about 3 hours trying to figure out why my function wasn't reading the page arguments from my hook_menu callback correctly. Added in &$form_state as the first parameter for my function and it works like a charm.

biswajeetparida’s picture

Incase of the below statement

<?php
    drupal_get_form('send_mymail', $subject, $toarray, $headers, $message, $namesarray, arg(2));
?>

The function, will be like this:

<?php
function send_mymail($form,&$form_state,$somesubj, $someto, $somehead, $somemessage, $somenames, $someurl) {
$form = array();
 // ......

$form['formmessage'] = array(
  '#type' => 'textfield',
  '#access' => false, 
  '#size' => 100,
  '#title' => 'message',
  '#description' => t('message'),
  '#value' => $somemessage, 
);
// So, for example, $somemessage is passed as the content of a form element
// ... all the way to the Submit butten :-)
?>
cfreixo’s picture

I was trying to figure this out, you solved it!! :D