Last updated February 19, 2008. Created by webchick on November 3, 2005.
Edited by ax. Log in to edit this page.
This example illustrates the creation of a simple form with a few fields and a submit button.
Before
<?php
function path_form($edit = '') {
$form .= form_textfield(t('Existing system path'), 'src', $edit['src'], 50, 64, t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'));
$form .= form_textfield(t('New path alias'), 'dst', $edit['dst'], 50, 64, t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'));
if ($edit['pid']) {
$form .= form_hidden('pid', $edit['pid']);
$form .= form_submit(t('Update alias'));
}
else {
$form .= form_submit(t('Create new alias'));
}
return $form
}
?>After
<?php
function path_form($edit = '') {
### Notice that name of the form field is declared as $form['src'], for example.
### The type of element is declared by using '#type'. If a form element needs a
### pre-filled value, use '#default_value'
$form['src'] = array(
'#type' => 'textfield',
'#title' => t('Existing system path'),
'#default_value' => $edit['src'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
);
$form['dst'] = array(
'#type' => 'textfield',
'#default_value' => $edit['dst'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
);
if ($edit['pid']) {
### Note the declaration of the types. Also, all values that are not subject to user input
### use the '#value' attribute
$form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
$form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
}
else {
$form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
}
### This is the master function that coordinates building, validating, executing, and displaying
### the constructed form. The first arg is the $form_id of the form, which by convention
### is usually the name of the function in which the form is created. Second arg is the
### constructed form array
return drupal_get_form('path_form', $form);
}
?>