Embed presentation
Downloaded 643 times






![Modules modify the form hook_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 ()) ; }}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-7-2048.jpg&f=jpg&w=240)
![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' ;](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-8-2048.jpg&f=jpg&w=240)
![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';](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-9-2048.jpg&f=jpg&w=240)
![Form validation Drupal 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'));}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-10-2048.jpg&f=jpg&w=240)
![Element specific form validation It 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.'); }}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-11-2048.jpg&f=jpg&w=240)

![Multipage form function 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: ... }](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-13-2048.jpg&f=jpg&w=240)



The document discusses Drupal form APIs, explaining how to build, modify, validate, and process forms programmatically. It covers creating form elements, hook_form_alter() for modifying existing forms, #after_build and validation functions, and creating multi-step forms using the #multistep property. The form API provides a flexible way to dynamically generate and manage forms without using hardcoded HTML.






![Modules modify the form hook_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 ()) ; }}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-7-2048.jpg&f=jpg&w=240)
![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' ;](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-8-2048.jpg&f=jpg&w=240)
![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';](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-9-2048.jpg&f=jpg&w=240)
![Form validation Drupal 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'));}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-10-2048.jpg&f=jpg&w=240)
![Element specific form validation It 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.'); }}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-11-2048.jpg&f=jpg&w=240)

![Multipage form function 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: ... }](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fdrupal-form-api-1193836426568444-5%2f75%2fDrupal-Form-Api-13-2048.jpg&f=jpg&w=240)

