15
Form API By : Payel Ghosh Mail : g.payel@puneitlabs.com

Drupal Form Api

Embed Size (px)

DESCRIPTION

this presentation explains the basics of drupal form api for drupal 5

Citation preview

Page 1: Drupal Form Api

Form API By : Payel Ghosh

Mail : [email protected]

Page 2: Drupal Form Api

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.

Page 3: Drupal Form Api

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

Page 4: Drupal Form Api

Understanding Form Processing

Page 5: Drupal Form Api

Form elements Textfield Textarea Password Select radios Checkboxes Value Hidden Date File Upload Fieldset Submit

Page 6: Drupal Form Api

Properties allowed in all elements #type #access #after_build #theme #prefix #suffix #title #weight ('#delta' => 10) #default_value

Page 7: Drupal Form Api

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()); }}

Page 8: Drupal Form Api

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';

Page 9: Drupal Form Api

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';

Page 10: Drupal Form Api

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'));}

Page 11: Drupal Form Api

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.');

}}

Page 12: Drupal Form Api

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

Page 13: Drupal Form Api

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:

...

}

Page 14: Drupal Form Api

Multipage form

Step - 1 step - 2

step - 3step - 4

Page 15: Drupal Form Api

Thank you