Drupal Form Api

46,438 views

Published on

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

Published in: Technology, Business
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
46,438
On SlideShare
0
From Embeds
0
Number of Embeds
5,361
Actions
Shares
0
Downloads
641
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

Drupal Form Api

  1. 1. Form API By : Payel Ghosh Mail : g.payel@ puneitlabs.com
  2. 2. Introducing form API <ul><li>Rather than output HTML, we create an array and let the engine generate the HTML. </li></ul><ul><li>Since we are dealing with a representation of the form as structured data, we can add, delete, reorder and change forms. </li></ul><ul><li>This is handy when you want to modify a form created by a different module easily. </li></ul><ul><li>Any form element can be mapped to any theme function. </li></ul><ul><li>Additional form validation or processing can be added to any form. </li></ul>
  3. 3. Form is easy to build <ul><li>drupal_get_form ( $mydetail_form) . Retrieves and builds mydetail_form </li></ul><ul><li> mydetail_form () function builds an array </li></ul><ul><li> mydetail_form_validate() validates the mydetail form </li></ul><ul><li> mydetail_form_submit() function processes </li></ul>
  4. 4. Understanding Form Processing
  5. 5. Form elements <ul><ul><li>Textfield </li></ul></ul><ul><ul><li>Textarea </li></ul></ul><ul><ul><li>Password </li></ul></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>radios </li></ul></ul><ul><ul><li>Checkboxes </li></ul></ul><ul><ul><li>Value </li></ul></ul><ul><ul><li>Hidden </li></ul></ul><ul><ul><li>Date </li></ul></ul><ul><ul><li>File Upload </li></ul></ul><ul><ul><li>Fieldset </li></ul></ul><ul><ul><li>Submit </li></ul></ul>
  6. 6. Properties allowed in all elements <ul><ul><li>#type </li></ul></ul><ul><ul><li>#access </li></ul></ul><ul><ul><li>#after_build </li></ul></ul><ul><ul><li>#theme </li></ul></ul><ul><ul><li>#prefix </li></ul></ul><ul><ul><li>#suffix </li></ul></ul><ul><ul><li>#title </li></ul></ul><ul><ul><li>#weight ('#delta' => 10) </li></ul></ul><ul><ul><li>#default_value </li></ul></ul>
  7. 7. Modules modify the form <ul><li>hook_form_alter( ) </li></ul><ul><li>this is the primary way to change, override the form that are created by modules other than your old one. </li></ul><ul><li>Any module that implements the form_alter() hook can modify anything in the form. </li></ul><ul><li>Before building the form form_alter() hook is called. </li></ul><ul><li>Birthdays.module </li></ul><ul><li>function birthdays_form_alter ($form_id, &$form) { if ($form_id == </li></ul><ul><li>'profile_field_form') { $form[ '#submit' ] = ( array ) $form [' #submit' ] + </li></ul><ul><li>array ( 'birthdays_profile_form_submit' => array ()) ; }} </li></ul>
  8. 8. Form modification after it’s built <ul><li>#after_build </li></ul><ul><li>#after_build is an optional array of functions to be called once the current form element has been built. </li></ul><ul><li>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’]. </li></ul><ul><li>Example: </li></ul><ul><li>image.module </li></ul><ul><li>$form[ 'thumbnail' ][' #after_build' ][] = 'image_form_add_thumbnail' ; </li></ul>
  9. 9. Finding theme function <ul><li>The benefits to having our own theme function are that we’re able to parse, munge, and add to $output as we please. </li></ul><ul><li>function theme_mydetail_form ($form) { </li></ul><ul><li>$output = drupal_render( $form ); </li></ul><ul><li>return $output;} </li></ul><ul><li>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. </li></ul><ul><li>$form ['#theme'] = ‘mydetail_form_special_theme'; </li></ul>
  10. 10. Form validation <ul><li>Drupal has a built-in mechanism for highlighting form elements that fail validation and displaying an error message to the user. </li></ul><ul><li>function mydetail_form_validate ($form_id,$form_values ){ </li></ul><ul><li>if ($form_values ['first_name']== 'abc') { </li></ul><ul><li>form_set_error ( t(' FIrstname is not valid'));} </li></ul>
  11. 11. Element specific form validation <ul><li>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. </li></ul><ul><li>$allowed_flavors = array (t('spicy'), t('sweet')); </li></ul><ul><li>$form ['flavor'] = array ( </li></ul><ul><li>'#type' => 'textfield', </li></ul><ul><li>'#title' => 'flavor', </li></ul><ul><li>'#validate' => array ('formexample_flavor_validate' => array( $allowed_flavors ))); </li></ul><ul><li>function formexample_flavor_validate( $element, $allowed_flavors ) { </li></ul><ul><li>if (!in_ array ( $element ['#value'], $allowed_flavors ) { </li></ul><ul><li>form_error( $element , t('You must enter spicy or sweet.'); </li></ul><ul><li>}} </li></ul>
  12. 12. Submit function <ul><li>The submit function is the function that takes care of actual form processing after the form has been validated. </li></ul><ul><li>It only executes if form validation passed completely </li></ul><ul><li>function mydetail_form_submit ($form, $form_values) { </li></ul><ul><li>// Now send user to node number 3. </li></ul><ul><li>return 'node/3'; </li></ul><ul><li>} </li></ul><ul><li>The redirection of the submit function can be overridden by defining a #redirect property in the form </li></ul>
  13. 13. Multipage form <ul><ul><li>function mymultiform_multiform( $form_values = NULL ) { </li></ul></ul><ul><ul><li>$form ['#multistep'] = TRUE ; </li></ul></ul><ul><ul><li>$step = isset ($form_values) ? ( int ) $form_values[ 'step' ] : 1; </li></ul></ul><ul><ul><li>$form ['step'] = array ( </li></ul></ul><ul><ul><li>'#type' => 'hidden', </li></ul></ul><ul><ul><li>'#value' => $step + 1 </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>switch ( $form_state [‘step’]) { </li></ul></ul><ul><ul><li>case 1: </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>case 2: </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>case 3: </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>} </li></ul></ul>
  14. 14. Multipage form Step - 1 step - 2 step - 3 step - 4
  15. 15. <ul><ul><ul><ul><ul><li>Thank you </li></ul></ul></ul></ul></ul>

×