Drupal Form Api
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Drupal Form Api

on

  • 50,913 views

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

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

Statistics

Views

Total Views
50,913
Views on SlideShare
45,574
Embed Views
5,339

Actions

Likes
13
Downloads
635
Comments
0

19 Embeds 5,339

http://amiworks.co.in 5040
http://www.slideshare.net 235
http://thuannvn.blogspot.com 13
http://translate.googleusercontent.com 12
http://webcache.googleusercontent.com 7
https://duckduckgo.com 5
http://webtimum.com 4
http://www.cms.rk.edu.pl 4
http://www.slideee.com 4
http://static.slideshare.net 4
http://209.85.229.132 2
http://saylinux.net 2
http://thuannvn.blogspot.in 1
http://localhost 1
http://209.85.173.132 1
http://74.125.39.132 1
http://209.85.129.132 1
http://74.125.47.132 1
http://static.slidesharecdn.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Drupal Form Api Presentation Transcript

  • 1. Form API By : Payel Ghosh Mail : g.payel@ puneitlabs.com
  • 2. 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.
  • 3. 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
  • 4. Understanding Form Processing
  • 5. Form elements
      • Textfield
      • Textarea
      • Password
      • Select
      • radios
      • Checkboxes
      • Value
      • Hidden
      • Date
      • File Upload
      • Fieldset
      • Submit
  • 6. Properties allowed in all elements
      • #type
      • #access
      • #after_build
      • #theme
      • #prefix
      • #suffix
      • #title
      • #weight ('#delta' => 10)
      • #default_value
  • 7. 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 ()) ; }}
  • 8. 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' ;
  • 9. 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';
  • 10. 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'));}
  • 11. 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.');
    • }}
  • 12. Submit function
    • The 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
  • 13. 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:
      • ...
      • }
  • 14. Multipage form Step - 1 step - 2 step - 3 step - 4
  • 15.
            • Thank you