Extreme Form Handling in Drupal

Drupal Form-Handling support goes far beyond just the documented part of so-called Forms API. You can do pretty much anything with forms in Drupal and you can use/display the forms anywhere. Here is an example. Let’s assume we want to construct a custom node type with custom fields, using CCK. Then we want to display this form into some non-standard page. To further complicate things, let’s assume we also want custom verification and processing routines.

Drupal Form-Handling support goes far beyond just the documented part of so-called Forms API. You can do pretty much anything with forms in Drupal and you can use/display the forms anywhere. Here is an example. Let’s assume we want to construct a custom node type with custom fields, using CCK. Then we want to display this form into some non-standard page. To further complicate things, let’s assume we also want custom verification and processing routines. And last but not least – we want to use full power of Drupal and write a minimal amount of code.Drupal Form-Handling support goes far beyond just the documented part of so-called Forms API. You can do pretty much anything with forms in Drupal

and you can use/display the forms anywhere.

Here is an example. Let’s assume we want to construct a custom node type with custom fields, using CCK. Then we want to display this form into some non-standard page. To further complicate things, let’s assume we also want custom verification and processing routines.

And last but not least – we want to use full power of Drupal and write a minimal amount of code. Following is a snippet demonstrating key points to achieving this task (thorough understanding of Drupal is required):

 <br>$node = new StdClass();<br>$node->type = OUR_NODE_TYPE;<br><br>$form = node_form ( $node ) ;<br>drupal_prepare_form( OUR_NODE_FORM_ID, $form ); //Let all form hooks run<br><br>//— Indicate proper processor, submit function, validate function etc.<br>$form['#action'] = '/node/add/'.OUR_NODE_TYPE;<br>$form['#validate'] = array('ourmodule_ourvalidationfunc' => array());<br>$form['#submit'] = array('ourmodule_oursubmitfunc' => array()); <br><br>ourmodule_form_disable_defaults( $form );<br><br>//— Render the form:<br>$form_html = drupal_render_form( OUR_NODE_FORM_ID, $form );<br><br>//— You just got a rendered <span class="caps">HTML</span> of the form in $form_html that you can<br>//— add to your output<br>.....<br><br>/**<br>* Disable some annoying default fields just for the<br>* kicks and to demo the power  of what can be done.<br>*/<br>function ourmodule_form_disable_defaults ( &$form ) {<br><br>      $form['attachments']['#collapsed'] = 0;<br>      $form['body_filter']['format'] = null;<br>      $form['format'] = array('#type' => 'hidden',<br>                              '#value' => 1);<br>      $form['log'] = null;<br>      $form['log'] = array('#type' => 'hidden',<br>                           '#value' => '');<br> <br>      $form['comment_settings'] = null;<br>        $form['comment'] = array('#type' => 'hidden',<br>                                 '#value' => 2); <br>      $form['menu'] = null;<br>     <br>      $form['body_filter']['body']['#rows'] = "5";<br> <br>      $authorname = $user->name;<br>        $form['author'] = null;<br>        $form['name'] = array('#type' => 'hidden',<br>                              '#value' => $authorname);<br>      $form['options'] = null;<br>      $form['path'] = null;<br>      $form['status'] = array('#type' => 'hidden',<br>                              '#value' => 1);<br>                             <br>      $form['preview']=null;<br><br>}

Please note that if your machine-readable name of the custom node type is something like “flight_schedule” then you will be defining the constants used in the code, as follows:

 <br>define ('OUR_NODE_TYPE', 'flight_schedule'); <br>define ('OUR_NODE_FORM_ID', 'flight_schedule_node_form');

Also, both the custom validation and submit functions have similar signature:

 <br>function ourmodule_ourvalidationfunc ($form_id, $form_values)<br>// and <br>function ourmodule_oursubmitfunc ($form_id, $form_values)

where $form_id is the Drupal form_id of the form and $form_values is an indexed array of submitted values. In the validation function you can set alert points using form_set_error() function like this:

 <br>form_set_error('field_flight_schedule', t('Flight Schedule ID must be unique.'));

where field_flight_schedule is a form element ID coming from a CCK field.

Also, for node forms you can call the original submit function from your submit function, so that you don’t have to duplicate things it already does. You do it with something like:

$original_return = node_form_submit($form_id, $form_values);

and you can end your _submit function with “return $original_return” so form submit redirects to where it would normally do.

The rest of the code should be self-explanatory, given a reader is proficient in Drupal. Also, you can leave comments here, if you have further questions.

Irakli Nadareishvili