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

$node = new StdClass();
$node->type = OUR_NODE_TYPE;

$form = node_form ( $node ) ;
drupal_prepare_form( OUR_NODE_FORM_ID, $form ); //Let all form hooks run

//-- Indicate proper processor, submit function, validate function etc.
$form['#action'] = '/node/add/'.OUR_NODE_TYPE;
$form['#validate'] = array('ourmodule_ourvalidationfunc' => array());
$form['#submit'] = array('ourmodule_oursubmitfunc' => array());  	

ourmodule_form_disable_defaults( $form );

//-- Render the form:
$form_html = drupal_render_form( OUR_NODE_FORM_ID, $form );

//-- You just got a rendered HTML of the form in $form_html that you can
//-- add to your output
.....

/**
* Disable some annoying default fields just for the 
* kicks and to demo the power  of what can be done.
*/
function ourmodule_form_disable_defaults ( &$form ) {

      $form['attachments']['#collapsed'] = 0;
      $form['body_filter']['format'] = null;
      $form['format'] = array('#type' => 'hidden',
                              '#value' => 1);
      $form['log'] = null;
      $form['log'] = array('#type' => 'hidden',
                           '#value' => '');
  
      $form['comment_settings'] = null;
        $form['comment'] = array('#type' => 'hidden',
                                 '#value' => 2);  
      $form['menu'] = null;
      
      $form['body_filter']['body']['#rows'] = "5";
  
      $authorname = $user->name;
        $form['author'] = null;
        $form['name'] = array('#type' => 'hidden',
                              '#value' => $authorname);
      $form['options'] = null;
      $form['path'] = null;
      $form['status'] = array('#type' => 'hidden',
                              '#value' => 1);
                              
      $form['preview']=null; 

}

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:

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

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

function ourmodule_ourvalidationfunc ($form_id, $form_values) 
// and
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:

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.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha Image: you will need to recognize the text in it.
Please type in the letters/numbers that are shown in the image above.