Drupal Forms Tutorial?



I am a newbie to the Drupal world and I want to create my own Drupal Forms. How are they different from HTML forms?

-- Tina



Drupal forms are represented as arrays that are executed by the theming engine. This theming engine renders it as HTML.

Drupal forms APIs are a set of functions that help you create and manage forms on Drupal website.

Drupal forms API not only helps you do this easily, but just as importantly, securely. Information from one form can be used in many ways and the process can be duplicated to create as many forms as you need.

Guide to Drupal Forms API

Reference: http://drupal.org/node/751826, http://drupal.org/node/262422, http://yadadrop.com/drupal-video/drupal-form-api-tutorial

Below its a example sample drupal form example using hook_form Drupal form API in Drupal 6 and how it is created as an array. You can refer Drupal API documentation on the format of that array. Later, this form is rendered using drupal_render drupal form API function.


<?php

function test_form($form_state) {
  
// Access log settings:
  
$options = array('1' => t('Enabled'), '0' => t('Disabled'));
  
$form['access'] = array(
    
'#type' => 'fieldset',
    
'#title' => t('Access log settings'),
    
'#tree' => TRUE,
  );
  
$form['access']['log'] = array(
    
'#type' => 'radios',
    
'#title' => t('Log'),
    
'#default_value' =>  variable_get('log'0),
    
'#options' => $options,
    
'#description' => t('The log.'),
  );
  
$period drupal_map_assoc(array(360010800216003240043200864001728002592006048001209600241920048384009676800), 'format_interval');
  
$form['access']['timer'] = array(
    
'#type' => 'select',
    
'#title' => t('Discard logs older than'),
    
'#default_value' => variable_get('timer'259200),
    
'#options' => $period,
    
'#description' => t('The timer.'),
  );
  
// Description
  
$form['details'] = array(
    
'#type' => 'fieldset',
    
'#title' => t('Details'),
    
'#collapsible' => TRUE,
    
'#collapsed' => TRUE,
  );
  
$form['details']['description'] = array(
    
'#type' => 'textarea',
    
'#title' => t('Describe it'),
    
'#default_value' =>  variable_get('description'''),
    
'#cols' => 60,
    
'#rows' => 5,
    
'#description' => t('Log description.'),
  );
  
$form['details']['admin'] = array(
    
'#type' => 'checkbox',
    
'#title' => t('Only admin can view'),
    
'#default_value' => variable_get('admin'0),
  );
  
$form['name'] = array(
    
'#type' => 'textfield',
    
'#title' => t('Name'),
    
'#size' => 30,
    
'#maxlength' => 64,
    
'#description' => t('Enter the name for this group of settings'),
  );
  
$form['hidden'] = array('#type' => 'value''#value' => 'is_it_here');
  
$form['submit'] = array('#type' => 'submit''#value' => t('Save'));
  return 
$form;
}

function 
test_page() {
  return 
drupal_get_form('test_form');
}
?>

<?php

For our above formwe could create a custom theming function as follows by rendering it:

<?
php
function theme_test_form($form) {
  
$output '';
  
$output .= drupal_render($form['name']);
  
$output .= '<div class="foo">';
  
$output .= drupal_render($form['access']);
  
$output .= '<div class="bar">';
  
$output .= drupal_render($form['details']);
  
$output .= '</div></div>';
  
$output .= drupal_render($form);
  return 
$output;
}
?>

Check this out: http://www.slideshare.net/teamphp/drupal-form-api