Fresh Blurbs

Extreme Form Handling in Drupal

Via: AgileApproach Blog

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

define ('OUR_NODE_TYPE', 'flight_schedule');
define ('OUR_NODE_FORM_ID', 'flight_schedule_node_form');

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:

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 that it already does. You do it with something like:

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

and you can and 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.

comments powered by Disqus