Drupal Form Api

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

7 comments

Comments 1 - 7 of 7 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

10 Favorites

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
            • Thank you

+ teamphpteamphp, 3 years ago

custom

20868 views, 10 favs, 10 embeds more stats

this presentation explains the basics of drupal for more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 20868
    • 19977 on SlideShare
    • 891 from embeds
  • Comments 7
  • Favorites 10
  • Downloads 430
Most viewed embeds
  • 876 views on http://amiworks.co.in
  • 4 views on http://static.slideshare.net
  • 4 views on http://thuannvn.blogspot.com
  • 1 views on http://static.slidesharecdn.com
  • 1 views on http://209.85.229.132

more

All embeds
  • 876 views on http://amiworks.co.in
  • 4 views on http://static.slideshare.net
  • 4 views on http://thuannvn.blogspot.com
  • 1 views on http://static.slidesharecdn.com
  • 1 views on http://209.85.229.132
  • 1 views on http://209.85.129.132
  • 1 views on http://74.125.39.132
  • 1 views on http://209.85.173.132
  • 1 views on http://localhost
  • 1 views on http://www.cms.rk.edu.pl

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories