You are on page 1of 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