drupal form api
DESCRIPTION
this presentation explains the basics of drupal form api for drupal 5TRANSCRIPT
Form API By : Payel Ghosh
Mail : [email protected]
Introducing form API Rather than output HTML, we create an array and let the engine generate
the HTML.
Since we are dealing with a representation of the form as structured data, we can add, delete, reorder and change forms.
This is handy when you want to modify a form created by a different module easily.
Any form element can be mapped to any theme function.
Additional form validation or processing can be added to any form.
Form is easy to build
drupal_get_form ($mydetail_form). Retrieves and builds mydetail_form
mydetail_form() function builds an array
mydetail_form_validate() validates the mydetail form
mydetail_form_submit() function processes
Understanding Form Processing
Form elements Textfield Textarea Password Select radios Checkboxes Value Hidden Date File Upload Fieldset Submit
Properties allowed in all elements #type #access #after_build #theme #prefix #suffix #title #weight ('#delta' => 10) #default_value
Modules modify the formhook_form_alter() • this is the primary way to change, override the form that are created by modules
other than your old one.• Any module that implements the form_alter() hook can modify anything in the
form.• Before building the form form_alter() hook is called.
Birthdays.module
function birthdays_form_alter($form_id, &$form) { if ($form_id ==
'profile_field_form') { $form['#submit'] = (array)$form['#submit'] +
array('birthdays_profile_form_submit' => array()); }}
Form modification after it’s built#after_build• #after_build is an optional array of functions to be called once the current form
element has been built.
• When the entire form has been built, a final call is made to the optional function whose names may be defined in $form [‘#after_build’].
Example:
image.module
$form['thumbnail']['#after_build'][] = 'image_form_add_thumbnail';
Finding theme function• The benefits to having our own theme function are that we’re able to parse, munge,
and add to $output as we please.
function theme_mydetail_form($form) {
$output = drupal_render($form);
return $output;}
• You can direct Drupal to use a function that does not match the formula “theme_ plus form ID name” by specifying a #theme property for a form.
$form['#theme'] = ‘mydetail_form_special_theme';
Form validationDrupal has a built-in mechanism for highlighting form elements that fail validation and displaying an error message to the user.
function mydetail_form_validate($form_id,$form_values){
if ($form_values['first_name']== 'abc') {
form_set_error ( t(' FIrstname is not valid'));}
Element specific form validationIt is possible to set validators for individual form elements To do that, set the #validate property for the element to an array with the name of the validation function as the key and any arguments you want to send along as the value.
$allowed_flavors = array(t('spicy'), t('sweet'));
$form['flavor'] = array(
'#type' => 'textfield',
'#title' => 'flavor',
'#validate' => array('formexample_flavor_validate' => array($allowed_flavors)));
function formexample_flavor_validate($element, $allowed_flavors) {
if (!in_array($element['#value'], $allowed_flavors) {
form_error($element, t('You must enter spicy or sweet.');
}}
Submit functionThe submit function is the function that takes care of actual form processing after the form has been validated.
It only executes if form validation passed completely
function mydetail_form_submit($form, $form_values) {
// Now send user to node number 3.
return 'node/3';
}
The redirection of the submit function can be overridden by defining a #redirect property in the form
Multipage formfunction mymultiform_multiform($form_values = NULL) {
$form['#multistep'] = TRUE;
$step = isset($form_values) ? (int) $form_values['step'] : 1;
$form['step'] = array(
'#type' => 'hidden',
'#value' => $step + 1
);switch ($form_state[‘step’]) {
case 1:
...
case 2:
...
case 3:
...
}
Multipage form
Step - 1 step - 2
step - 3step - 4
Thank you