В одной задаче потребовалось добавить свои атрибуты к option в select. Посмотрел в инете и нашел пару годных решений. Скопилировал, их написал кусочек кода.
/**
* theme_select
* @param $variables
* @return string
*/
function bootstrap_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']) . '>' . bootstrap_form_select_options($element) . '</select>';
}
function bootstrap_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)) {
$options .= '<optgroup label="' . check_plain($key) . '">';
$options .= bootstrap_form_select_options($element, $choice);
$options .= '</optgroup>';
}
elseif (is_object($choice)) {
$options .= bootstrap_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 = '';
}
if (!empty($element['#options_attributes'][$key])) {
$options_attributes = ' ' . drupal_attributes($element['#options_attributes'][$key]);
}
else {
$options_attributes = '';
}
$options .= '<option value="'. check_plain($key) .'"' . $selected . $options_attributes .'>'. check_plain($choice).'</option>';
}
}
return $options;
}bootstrap_select - это переопределение функции темизации theme_select. bootstrap - так называется тема (машинное имя).
Весь код пишем в template.php.
Код который ниже используем для заполнения дополнительных атрибутов в select
$payment_methods = commerce_payment_methods();
$options_attributes = array();
foreach($payment_methods as $key => $value){
if($value['active']){
$payment_methods_options[$key] = $value['title'];
$options_attributes[$key] = array('data-factors' => '0.63063063063063,1.063829787234,1', 'data-unit' => $key);
}
}
$form['payment_methods'] = array(
'#title' => 'Способ оплаты',
'#type' => 'select',
'#options' => $payment_methods_options,
'#options_attributes' => $options_attributes
);