Community Documentation

Building Individual Fields

Last updated October 22, 2012. Created by siruguri on January 5, 2009.
Edited by kiamlaluno, drupalshrek, Jenya, cluke009. Log in to edit this page.

An individual form element is specified as an array of properties that define the element. The key used to insert the element in the $form array will be how the element is referred to in the form workflow.

For example, to create a textfield element in a form, you would use the following properties (only the type property is required to create a textfield):

<?php
$form
['my_text_field'] = array(
 
'#type' => 'textfield',
 
'#title' => t('Enter some text here:'),
 
'#default_value' => $node->title,
 
'#size' => 60,
 
'#maxlength' => 128,
 
'#required' => TRUE
);
?>

The key will also be the value of the element's name attribute in the rendered HTML, so do not use spaces in the keys of the $form array.

A full list of how to specify the various input types of a form (radio, check boxes, drop-down selectors, etc.) is listed here: Input Types in the Forms API for Drupal 6.x

With just the input types and a submit button, you can create most simple, one-stage forms! You really don't need to learn much more to create forms, apart from how to deal with the form data once it is posted.

Here is an example of creating a submit button:

<?php
$form
['my_submit_button'] = array(
 
'#type' => 'submit',
 
'#value' => t('Import')
);
?>

Remember to enclose all strings in the form that are going to be described externally within the t() function, so that your form can be translated within the Drupal framework!

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here