View source
<?php
function multiple_selects_theme() {
return array(
'multiple_selects_none' => array(
'variables' => array(
'instance' => NULL,
'option' => NULL,
),
),
);
}
function theme_multiple_selects_none($variables) {
$instance = $variables['instance'];
$option = $variables['option'];
$output = $option == 'option_none' ? t('- None -') : t('- Select a value -');
return $output;
}
function multiple_selects_field_widget_info() {
return array(
'multiple_selects' => array(
'label' => t('Multiple Selects list'),
'field types' => array(
'entityreference',
'node_reference',
'user_reference',
'taxonomy_term_reference',
'list_integer',
'list_float',
'list_text',
'list_boolean',
'commerce_product_reference',
),
),
);
}
function multiple_selects_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value_key = key($field['columns']);
$type = "select";
$multiple = 1;
$required = $element['#required'];
$has_value = isset($items[0][$value_key]);
$properties = _options_properties($type, $multiple, $required, $has_value);
$entity_type = $element['#entity_type'];
$entity = $element['#entity'];
$options = _multiple_selects_get_options($field, $instance, $properties, $entity_type, $entity);
$element['#title'] = $instance['label'];
$element['#title_display'] = 'none';
$widget = $element;
$widget += array(
'#type' => 'select',
'#default_value' => isset($items[$delta][$value_key]) ? $items[$delta][$value_key] : NULL,
'#options' => $options,
'#element_validate' => array(
'multiple_selects_widget_validate',
),
'#value_key' => $value_key,
'#properties' => $properties,
);
$element[$value_key] = $widget;
return $element;
}
function multiple_selects_widget_validate($element, &$form_state) {
$value_key = $element['#value_key'];
$properties = $element['#properties'];
$value = $element['#value'];
if ($element['#required'] && $element['#value'] === '_none') {
form_error($element, t('!name field is required.', array(
'!name' => $element['#title'],
)));
}
if ($properties['empty_option']) {
if ($value === '_none') {
form_set_value($element, NULL, $form_state);
}
}
}
function multiple_selects_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message']);
}
function _multiple_selects_get_options($field, $instance, $properties, $entity_type, $entity) {
$options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity_type, $entity);
_options_prepare_options($options, $properties);
if (!$properties['optgroups']) {
$options = options_array_flatten($options);
}
if ($properties['empty_option']) {
$label = theme('multiple_selects_none', array(
'instance' => $instance,
'option' => $properties['empty_option'],
));
$options = array(
'_none' => $label,
) + $options;
}
return $options;
}