You are here

function multiple_selects_field_widget_form in Multiple Selects 7

Implements hook_field_widget_form().

File

./multiple_selects.module, line 61

Code

function multiple_selects_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Abstract over the actual field columns, to allow different field types to
  // reuse those widgets.
  $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'];

  // Prepare the list of options.
  $options = _multiple_selects_get_options($field, $instance, $properties, $entity_type, $entity);

  // Enforcing the element to have the value of the field instance label.
  $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;
}