Forms API Inside Out
  Or, How I Learned to Stop Worrying
      and Love the Nested Arrays




                           ...
The Olden Days
In the beginning, there was HTML
Then, there were helper functions
Each form had to reinvent workflow
Each f...
Drupal’s Answer: Forms API

Build forms as structured data
Make the workflow automatic
Make ‘doing the right thing’ easy
Wh...
So, What’s It Look Like?
function my_form() {
  $form = array();

    $form['foo'] = array(
       '#type' => 'textarea',
...
So, What’s It Look Like?




                           5
How to Use Your Form
drupal_get_form('my_form')

my_form()

my_form_validate(...)

my_form_submit(...)
                   ...
Behind the Scenes

    http://foo.com/my-form
                 my_form_page()
      drupal_get_form('my_form')
           ...
Behind the Scenes (Part 2)

   Here’s that form you gave me
       drupal_get_form('my_form')
                   $_POST ha...
Recap, With Kittens
Call drupal_get_form(‘my_form’)
The my_form() function builds an array
   Drupal sanity checks $_POST
...
Here’s the Magic
Build the initial form definition
drupal_process_form()
   drupal_build_form()
       hook_form_alter()
  ...
Here’s the Magic
_form_builder() does a lot!
Handles defaults
Weaves $_POST into the form
Builds $form_values
Inserts secu...
“Special” Bits

hook_elements()
hook_form_alter()
drupal_execute()



                    12
Current Limitations

Dynamic AHAH/AJAX
Wizard-style forms
Too many special cases
AHAH/AJAX security!


                   ...
The Future of Forms API

Form chunks
State management
AHAH/AJAX security



                          14
Whee!




        15
Upcoming SlideShare
Loading in...5
×

Form API Intro

2,513

Published on

An introduction to Drupal's FormAPI, as of version 5.

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

No Downloads
Views
Total Views
2,513
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
95
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Form API Intro

  1. 1. Forms API Inside Out Or, How I Learned to Stop Worrying and Love the Nested Arrays 1
  2. 2. The Olden Days In the beginning, there was HTML Then, there were helper functions Each form had to reinvent workflow Each form had to reinvent security The Node Form made Baby Jesus cry 2
  3. 3. Drupal’s Answer: Forms API Build forms as structured data Make the workflow automatic Make ‘doing the right thing’ easy When everything is done, render to HTML 3
  4. 4. So, What’s It Look Like? function my_form() { $form = array(); $form['foo'] = array( '#type' => 'textarea', '#title' => t('Your foo'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit yo foo'), ); return $form; } 4
  5. 5. So, What’s It Look Like? 5
  6. 6. How to Use Your Form drupal_get_form('my_form') my_form() my_form_validate(...) my_form_submit(...) 6
  7. 7. Behind the Scenes http://foo.com/my-form my_form_page() drupal_get_form('my_form') my_form() (Here, magic happens*) drupal_render($form) 7
  8. 8. Behind the Scenes (Part 2) Here’s that form you gave me drupal_get_form('my_form') $_POST has data! my_form() my_form_validate(...) my_form_submit(...) 8
  9. 9. Recap, With Kittens Call drupal_get_form(‘my_form’) The my_form() function builds an array Drupal sanity checks $_POST The my_form_validate() function validates The my_form_submit() function processes The drupal_render() function outputs the form. T. Keller 9
  10. 10. Here’s the Magic Build the initial form definition drupal_process_form() drupal_build_form() hook_form_alter() _form_builder() 10
  11. 11. Here’s the Magic _form_builder() does a lot! Handles defaults Weaves $_POST into the form Builds $form_values Inserts security tokens 11
  12. 12. “Special” Bits hook_elements() hook_form_alter() drupal_execute() 12
  13. 13. Current Limitations Dynamic AHAH/AJAX Wizard-style forms Too many special cases AHAH/AJAX security! 13
  14. 14. The Future of Forms API Form chunks State management AHAH/AJAX security 14
  15. 15. Whee! 15
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×