Form API 101
Intro to building Forms in Drupal using PHP	

!
!
presented by Chris Charlton	

http://xtnd.us
Drupal uses PHP Arrays for forms
• Easy to Add, Remove, or Alter:	

• Form fields & their attributes	

• Validation & Submi...
Form Field Types
• Text input	

• File input/upload	

• Select Lists/Menu/Dropdowns	

• Checkboxes, Radiobuttons	

• Lots ...
Before we talk Drupal...
• <form> HTML tag 	

• PHP code summary	

• Basics	

• Arrays
<form>
<form id="unique-form-id" action="form_action.php">

First name: <input type="text" name="firstname" />

Last name:...
PHP required... but how much?
• Basic PHP	

• Strings & Numbers	

• Variables	

• Functions	

!
!
• Arrays	

• Keys	

• Va...
PHP Basics
Strings - “this is a string”	
Numbers - 1234567890	

Variables - $long_var_name = 123;	
Functions - hook_form()
PHP Arrays
<?php

// Declare new basic array of numbers up to ten

$array_of_numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8);

...
Mixed Key PHP Array
<?php

// Declare multidimensional array with mixed keys

$mixed_array = array(

0 => ‘index zero has ...
Multidimensional PHP Array
<?php

// Declare multidimensional array with mixed keys

$multidim_array = array(

0 => array(...
Form API
a.k.a “FAPI”
Form API - Textfield
<?php



$form[‘firstname’] = array(

‘#type‘ => ‘textfield’,

‘#title‘ => t(‘First name’),

);



Form parts you build
<form id="simple-form" action="">

First name: <input type="text" name="firstname" />

Last name: <in...
Same form in PHP using
Drupal’s Form API<?php	
// Simple function returns simple form array

function simple_form() {

$fo...
Homework #1:
Try out all form types
Homework #2:
Read and learn these...
hook_forms()
• Hook that lets you declare all known forms
within your module and assigns Form ID’s	

• “hook_” replaced “y...
hook_form_alter()
• The hook allowing you to alter all forms	

• “hook_” replaced “your_module_name_” 	

• Specific form: h...
Learn more
• http://api.drupal.org	

• http://drupal.org/project/examples	

• http://drupal.org	

• http://drupalcampla.com
Upcoming SlideShare
Loading in …5
×

Drupal Form API 101 (PHP) - DrupalCamp LA 2012

411 views
342 views

Published on

Ready to learn how forms are built or manipulated in Drupal using PHP code? Take your Drupal skills up to the next level by getting comfortable with the basic PHP needed to build and manipulate forms using the Form API (also known as FAPI).

What you will learn:

What form fields are possible using the Form API
How to build a new form from scratch that appears at a custom URL
How to override (manipulate) an existing form
How to make fields disabled, appear in a different order, or save their data elsewhere
How to debug a form
Basic info on creating CUSTOM form fields & items
Form API info good for both Drupal 7 & 6
NOTE: To enjoy this session fully you should be comfortable with HTML & CSS, and have some basic comfort or understanding of Arrays and/or PHP.

Published in: Software
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
411
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Drupal Form API 101 (PHP) - DrupalCamp LA 2012

  1. 1. Form API 101 Intro to building Forms in Drupal using PHP ! ! presented by Chris Charlton http://xtnd.us
  2. 2. Drupal uses PHP Arrays for forms • Easy to Add, Remove, or Alter: • Form fields & their attributes • Validation & Submit handling • Form tag & attributes • Write less code • Reduces markup errors
  3. 3. Form Field Types • Text input • File input/upload • Select Lists/Menu/Dropdowns • Checkboxes, Radiobuttons • Lots of types, or make your own!
  4. 4. Before we talk Drupal... • <form> HTML tag • PHP code summary • Basics • Arrays
  5. 5. <form> <form id="unique-form-id" action="form_action.php">
 First name: <input type="text" name="firstname" />
 Last name: <input type="text" name="lastname" />
 <input type="submit" value="Submit" />
 </form> ! ! !
  6. 6. PHP required... but how much? • Basic PHP • Strings & Numbers • Variables • Functions ! ! • Arrays • Keys • Values • Multidimensional/Tree
  7. 7. PHP Basics Strings - “this is a string” Numbers - 1234567890 Variables - $long_var_name = 123; Functions - hook_form()
  8. 8. PHP Arrays <?php
 // Declare new basic array of numbers up to ten
 $array_of_numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8);
 $array_of_numbers[] = 9; // Add an array item
 $array_of_numbers[] = 10; // Add another item
 
 //Declare new basic array of strings
 $array_of_strings = array(‘A’, ‘b’, ‘C’, ‘d’, ‘E’); !
  9. 9. Mixed Key PHP Array <?php
 // Declare multidimensional array with mixed keys
 $mixed_array = array(
 0 => ‘index zero has this string value’,
 1 => ‘index one has this similar string value’,
 ‘two’ => ‘index two has a string key’,
 ‘three’ => ‘index three also has a string key’,
 4 => ‘index four is a numeric key like index 0 & 1’
 ); !
  10. 10. Multidimensional PHP Array <?php
 // Declare multidimensional array with mixed keys
 $multidim_array = array(
 0 => array(
 “person0” => “Jay”,
 “person1” => “Silent Bob”,
 “person2” => “Chris”
 ),
 1 => ‘index one has this similar string value’,
 ‘two’ => ‘index two has a string key’,
 ‘three’ => ‘index three also has a string key’,
 4 => ‘index four is a numeric key like index 0 & 1’
 );
  11. 11. Form API a.k.a “FAPI”
  12. 12. Form API - Textfield <?php
 
 $form[‘firstname’] = array(
 ‘#type‘ => ‘textfield’,
 ‘#title‘ => t(‘First name’),
 );
 

  13. 13. Form parts you build <form id="simple-form" action="">
 First name: <input type="text" name="firstname" />
 Last name: <input type="text" name="lastname" />
 <input type="submit" value="Submit" />
 </form> ! ! !
  14. 14. Same form in PHP using Drupal’s Form API<?php // Simple function returns simple form array
 function simple_form() {
 $form = array();
 $form[‘firstname’] = array(
 ‘#type‘ => ‘textfield’,
 ‘#title‘ => t(‘First name’),
 );
 $form[‘lastname’] = array(
 ‘#type‘ => ‘textfield’,
 ‘#title‘ => t(‘Last name’),
 );
 $form[‘submit’] = array(
 ‘#type‘ => ‘submit’,
 ‘#title‘ => t(‘Submit’),
 );
 return $form;
 } <?php // Call in that custom form
 function custom_form_page() {
 return drupal_get_form(‘simple_form’);
 }
  15. 15. Homework #1: Try out all form types
  16. 16. Homework #2: Read and learn these...
  17. 17. hook_forms() • Hook that lets you declare all known forms within your module and assigns Form ID’s • “hook_” replaced “your_module_name_”
  18. 18. hook_form_alter() • The hook allowing you to alter all forms • “hook_” replaced “your_module_name_” • Specific form: hook_form_FORM_ID_alter()
  19. 19. Learn more • http://api.drupal.org • http://drupal.org/project/examples • http://drupal.org • http://drupalcampla.com

×