KEMBAR78
Drupal Form API 101 (PHP) - DrupalCamp LA 2012 | PDF
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 & Submit handling	

• Form tag & attributes	

• Write less code	

• Reduces markup errors
Form Field Types
• Text input	

• File input/upload	

• Select Lists/Menu/Dropdowns	

• Checkboxes, Radiobuttons	

• Lots of types, or make your own!
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: <input type="text" name="lastname" />

<input type="submit" value="Submit" />

</form>	
!
!
!
PHP required... but how much?
• Basic PHP	

• Strings & Numbers	

• Variables	

• Functions	

!
!
• Arrays	

• Keys	

• Values	

• Multidimensional/Tree
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);

$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’);	
!
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’

);	
!
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’

);
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: <input type="text" name="lastname" />

<input type="submit" value="Submit" />

</form>	
!
!
!
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’);

}
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 “your_module_name_”
hook_form_alter()
• The hook allowing you to alter all forms	

• “hook_” replaced “your_module_name_” 	

• Specific form: hook_form_FORM_ID_alter()
Learn more
• http://api.drupal.org	

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

• http://drupal.org	

• http://drupalcampla.com

Drupal Form API 101 (PHP) - DrupalCamp LA 2012

  • 1.
    Form API 101 Introto building Forms in Drupal using PHP ! ! presented by Chris Charlton http://xtnd.us
  • 2.
    Drupal uses PHPArrays 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.
    Form Field Types •Text input • File input/upload • Select Lists/Menu/Dropdowns • Checkboxes, Radiobuttons • Lots of types, or make your own!
  • 4.
    Before we talkDrupal... • <form> HTML tag • PHP code summary • Basics • Arrays
  • 5.
    <form> <form id="unique-form-id" action="form_action.php">
 Firstname: <input type="text" name="firstname" />
 Last name: <input type="text" name="lastname" />
 <input type="submit" value="Submit" />
 </form> ! ! !
  • 6.
    PHP required... buthow much? • Basic PHP • Strings & Numbers • Variables • Functions ! ! • Arrays • Keys • Values • Multidimensional/Tree
  • 7.
    PHP Basics Strings -“this is a string” Numbers - 1234567890 Variables - $long_var_name = 123; Functions - hook_form()
  • 8.
    PHP Arrays <?php
 // Declarenew 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.
    Mixed Key PHPArray <?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.
    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.
  • 12.
    Form API -Textfield <?php
 
 $form[‘firstname’] = array(
 ‘#type‘ => ‘textfield’,
 ‘#title‘ => t(‘First name’),
 );
 

  • 13.
    Form parts youbuild <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.
    Same form inPHP 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.
    Homework #1: Try outall form types
  • 16.
    Homework #2: Read andlearn these...
  • 17.
    hook_forms() • Hook thatlets you declare all known forms within your module and assigns Form ID’s • “hook_” replaced “your_module_name_”
  • 18.
    hook_form_alter() • The hookallowing you to alter all forms • “hook_” replaced “your_module_name_” • Specific form: hook_form_FORM_ID_alter()
  • 19.
    Learn more • http://api.drupal.org •http://drupal.org/project/examples • http://drupal.org • http://drupalcampla.com