You are here

README.txt in Form Options Attributes 7

# Form Options Attributes Module

## Overview

This module adds the ability to specify attributes for individual options
on Drupal Form API elements of the types select, checkboxes, and radios.

This is an API module, with no user interface. You would only need this
module if another module you are using requires it or if you are programming
a custom form that requires attributes on select <option> children, radio
buttons within a radios element, or checkbox elements within an checkboxes
element.


## Usage

To add attributes to a form element's options, add an '#options_attributes'
key to the form element definition. The #options_attributes value should be
an array with keys that match the keys in the #options value array. The values
in the #options_attributes array are formatted like the main #attributes array.

Example:

$form['states'] = array(
  '#type' => 'select',
  '#title' => t('States'),
  '#options' => array(
    'AL' => t('Alabama'),
    'AK' => t('Alaska'),
    'AZ' => t('Arizona'),
    'AR' => t('Arkansas'),
    // ...
    'WI' => t('Wisconsin'),
    'WY' => t('Wyoming'),
  ),
  '#options_attributes' => array(
    'AL' => array('class' => array('southeast'), 'data-bbq-meat' => 'pork'),
    'AK' => array('class' => array('non-contiguous'), 'data-bbq-meat' => 'crab'),
    'AZ' => array('class' => array('southwest'), 'data-bbq-meat' => 'rattlesnake'),
    'AR' => array('class' => array('south'), 'data-bbq-meat' => 'beef'),
    // ...
    'WI' => array('class' => array('midwest'), 'data-bbq-meat' => 'cheese'),
    'WY' => array('class' => array('flyover'), 'data-bbq-meat' => 'bison'),
  ),
  '#attributes' => array('class' => array('states-bbq-selector')),
);

File

README.txt
View source
  1. # Form Options Attributes Module
  2. ## Overview
  3. This module adds the ability to specify attributes for individual options
  4. on Drupal Form API elements of the types select, checkboxes, and radios.
  5. This is an API module, with no user interface. You would only need this
  6. module if another module you are using requires it or if you are programming
  7. a custom form that requires attributes on select
  8. buttons within a radios element, or checkbox elements within an checkboxes
  9. element.
  10. ## Usage
  11. To add attributes to a form element's options, add an '#options_attributes'
  12. key to the form element definition. The #options_attributes value should be
  13. an array with keys that match the keys in the #options value array. The values
  14. in the #options_attributes array are formatted like the main #attributes array.
  15. Example:
  16. $form['states'] = array(
  17. '#type' => 'select',
  18. '#title' => t('States'),
  19. '#options' => array(
  20. 'AL' => t('Alabama'),
  21. 'AK' => t('Alaska'),
  22. 'AZ' => t('Arizona'),
  23. 'AR' => t('Arkansas'),
  24. // ...
  25. 'WI' => t('Wisconsin'),
  26. 'WY' => t('Wyoming'),
  27. ),
  28. '#options_attributes' => array(
  29. 'AL' => array('class' => array('southeast'), 'data-bbq-meat' => 'pork'),
  30. 'AK' => array('class' => array('non-contiguous'), 'data-bbq-meat' => 'crab'),
  31. 'AZ' => array('class' => array('southwest'), 'data-bbq-meat' => 'rattlesnake'),
  32. 'AR' => array('class' => array('south'), 'data-bbq-meat' => 'beef'),
  33. // ...
  34. 'WI' => array('class' => array('midwest'), 'data-bbq-meat' => 'cheese'),
  35. 'WY' => array('class' => array('flyover'), 'data-bbq-meat' => 'bison'),
  36. ),
  37. '#attributes' => array('class' => array('states-bbq-selector')),
  38. );