Drupal by default doesn't allow for this, so you'll need to do two things:
- Override theme_select()
- Create your own callback the create the options
- Change the way you define your select options in your form definition
Overriding theme_select():
function THEME_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
return '<select' . drupal_attributes($element['#attributes']) . '>' . custom_form_select_options($element) . '</select>';
}
Create your own callback the create the options:
function custom_form_select_options($element, $choices = NULL) {
if (!isset($choices)) {
$choices = $element['#options'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice) && !isset($choice['data'])) {
$options .= '<optgroup label="' . check_plain($key) . '">';
$options .= form_select_options($element, $choice);
$options .= '</optgroup>';
}
elseif (is_object($choice)) {
$options .= form_select_options($element, $choice->option);
}
else {
$key = (string) $key;
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
$options .= '<option value="' . check_plain($key) . '"' . $selected;
if(is_array($choice) && isset($choice['disabled']) && $choice['disabled'])
{
$options .= ' disabled';
}
$choice = is_array($choice) && isset($choice['data']) ? $choice['data'] : $choice;
$options .= '>' . check_plain($choice) . '</option>';
}
}
return $options;
}
Change the way you define your select options in your form definition. Turn the option into an array with two keys:
- data: The display value of the element
- disabled: A boolean indicating whether to disable the element
Example:
$form['select'] = array(
'#title' => t('Pick something'),
'#type' => 'select',
'#options' => array(
'yes' => array('data' => t('Yes'), 'disabled' => TRUE),
'no' => t('No'),
),
);