Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Form Options Attributes

9,553 sites Security covered Drupal 10–11
View on drupal.org

This module provides a way to add custom attributes to individual options within Drupal form elements like select boxes, radio buttons, and checkboxes. It's an API module that developers can use when building forms that require fine-grained control over form element options.

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 needs attributes on select <option> children, individual radio buttons within a radios element, or individual checkbox elements within a checkboxes group 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 should be formatted like the main #attributes array.

Example:

<?php  
$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' => 'salmon'),
    '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')),
);
?>

Label and wrapper attributes

Individual attributes for each option's label and wrapper can be set using the '#options_wrapper_attributes' and '#options_label_attributes' keys in the same vein as the '#options_attributes' key. This is only supported for 'radios' and 'checkboxes' elements.

Core Issue

This module exists because of a lack of the options attribute functionality in Drupal core. There is an issue related to adding this to core here: #342316: Introduce proper Form API #types for 'option' and 'optgroup', and make #options consistent.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Nov 2025
Latest release
9 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Slowing

Releases

Version Type Core Release date
2.1.1 Stable 10–11 Nov 5, 2025