Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
240 views

Drupal Form API Cheat Sheet

The document describes various form elements that can be used in Drupal's Form API, including text fields, text areas, checkboxes, radios, buttons, file uploads, fieldsets and markup. It provides examples of how to define each element type using the #type, #title, #default_value and other properties. Additional useful Form API options are also listed at the end such as #title_display, #access, #required, #attributes and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Drupal Form API Cheat Sheet

The document describes various form elements that can be used in Drupal's Form API, including text fields, text areas, checkboxes, radios, buttons, file uploads, fieldsets and markup. It provides examples of how to define each element type using the #type, #title, #default_value and other properties. Additional useful Form API options are also listed at the end such as #title_display, #access, #required, #attributes and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Drupal 7 - Form API [CHEAT SHEET]

1. Text Field

$form['text'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => 'Default Value',
'#size' => 60,
'#maxlength' => 128,
);

2. Text Area

// Minimal text area


$form['text_area'] = array(
'#type' => 'textarea',
'#title' => 'Content',
'#default_value' => 'Default Value',
'#maxlength' => 128,
);

// Formatted Full HTML text area


$form['text_area'] = array(
'#type' => 'text_format',
'#title' => 'Content',
'#default_value' => 'Default Value',
'#format' => 'full_html',
'#base_type' => 'textarea',
'#maxlength' => 1000,
);

3. Checkbox

$form['checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Content'),
);

dishanphilips.com - https://www.drupal.org/u/dphilips
4. Checkboxes

$form['checkboxes'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkboxes',
'#default_value' => array('value1','value2'),
'#options' => array(
'value1' => 'Label 1',
'value2' => 'Label 2',
'value3' => 'Label 3',
),
'#multiple' => true,
);

5. Radio

$form['radios'] = array(
'#type' => 'radios',
'#title' => 'Radios',
'#default_value' => array('value1'),
'#options' => array(
'value1' => 'Label 1',
'value2' => 'Label 2',
'value3' => 'Label 3',
),
);

6. Button

$form['button'] = array(
'#type' => 'button',
'#value' => t('Button'),
);

7. Submit

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);

dishanphilips.com - https://www.drupal.org/u/dphilips
8. Password

$form['password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#size' => 10,
'#maxlength' => 64,
);

9. Password Confirm

$form['password'] = array(
'#type' => 'password_confirm',
'#title' => t('Password Confirm'),
'#size' => 10,
);

10. Image Button

$form['image'] = array(
'#type' => 'managed_file',
'#name' => 'custom_image_file',
'#title' => t('Image'),
'#size' => 100,
'#upload_location' => 'public://',
);

11. File

$form['file'] = array(
'#type' => 'file',
'#name' => 'custom_image_file',
'#title' => t('Upload File'),
'#size' => 100,
);

12. Field Set

$form['filedset'] = array(
'#type' => 'fieldset',
'#title' => t('Fieldset'),
'#collapsible' => true,
'#collapsed' => false,
);

dishanphilips.com - https://www.drupal.org/u/dphilips
13. Markup

$form['markup'] = array(
'#type' => 'item',
'#title' => t('Markup'),
'#markup' => '<p>Markup</p>',
);

Useful Form API Options

1. '#title_display' => 'invisible'/'inline'

2. '#access' => true/false

3. '#required' => true/false

4. '#attributes' => array(


'class'=>array(
'class1',
'class2',
),
'placeholder'=>'Place Holder')

5. '#id' => 'elementId'

6. '#name' => 'elementName'

7. '#prefix' => '<div>Some Prefix'

8. '#suffix' => 'Some Suffix</div>'

9. '#required' => true/false

10. '#weight' => -1/10

dishanphilips.com - https://www.drupal.org/u/dphilips

You might also like