form api intro
DESCRIPTION
An introduction to Drupal's FormAPI, as of version 5.TRANSCRIPT
![Page 1: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/1.jpg)
Forms API Inside OutOr, How I Learned to Stop Worrying
and Love the Nested Arrays
1
![Page 2: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/2.jpg)
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
![Page 3: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/3.jpg)
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
![Page 4: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/4.jpg)
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
![Page 5: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/5.jpg)
So, What’s It Look Like?
5
![Page 6: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/6.jpg)
How to Use Your Form
drupal_get_form('my_form')
my_form()
my_form_validate(...)
my_form_submit(...)
6
![Page 7: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/7.jpg)
http://foo.com/my-form
Behind the Scenes
my_form_page()drupal_get_form('my_form')
my_form()(Here, magic happens*)
drupal_render($form)
7
![Page 8: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/8.jpg)
Behind the Scenes (Part 2)
Here’s that form you gave me
my_form()
my_form_submit(...)
my_form_validate(...)
$_POST has data!
drupal_get_form('my_form')
8
![Page 9: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/9.jpg)
Recap, With KittensCall 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
![Page 10: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/10.jpg)
Build the initial form definition
drupal_process_form()
drupal_build_form()
hook_form_alter()
_form_builder()
Here’s the Magic
10
![Page 11: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/11.jpg)
Here’s the Magic
_form_builder() does a lot!
Handles defaults
Weaves $_POST into the form
Builds $form_values
Inserts security tokens
11
![Page 12: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/12.jpg)
“Special” Bits
hook_elements()
hook_form_alter()
drupal_execute()
12
![Page 13: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/13.jpg)
Current Limitations
Dynamic AHAH/AJAX
Wizard-style forms
Too many special cases
AHAH/AJAX security!
13
![Page 14: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/14.jpg)
The Future of Forms API
Form chunks
State management
AHAH/AJAX security
14
![Page 15: Form API Intro](https://reader033.vdocuments.us/reader033/viewer/2022050905/54b72a724a79599d2a8b45f2/html5/thumbnails/15.jpg)
Whee!
15