You are here

function select_or_other_field_widget_form_prepare_options in Select (or other) 7.2

Same name and namespace in other branches
  1. 7.3 select_or_other.field_widget.inc \select_or_other_field_widget_form_prepare_options()

Prepare options for the widget list.

1 call to select_or_other_field_widget_form_prepare_options()
select_or_other_field_widget_form in ./select_or_other.field_widget.inc
Implements hook_field_widget_form().

File

./select_or_other.field_widget.inc, line 105
The Select (or other) field widget.

Code

function select_or_other_field_widget_form_prepare_options($field, $instance, $has_value = FALSE) {
  $options = array();
  $settings =& $instance['widget']['settings'];
  $list = _select_or_other_field_widget_get_available_options($settings);
  foreach ($list as $key => $opt) {
    if (is_array($opt)) {
      $optgroup_options = array();
      foreach ($opt as $optgroup_key => $optgroup_opt) {
        select_or_other_field_widget_form_prepare_option($optgroup_options, $optgroup_key, $optgroup_opt, $settings);
        $options[$key] = $optgroup_options;
      }
    }
    else {
      select_or_other_field_widget_form_prepare_option($options, $key, $opt, $settings);
    }
  }
  $required = isset($instance['required']) && $instance['required'];
  $multiple = $instance['widget']['type'] == 'select_or_other' && $field['cardinality'] == -1 || $instance['widget']['type'] == 'select_or_other_sort';
  $multiple_checkbox = $instance['widget']['type'] == 'select_or_other_buttons' && $field['cardinality'] == -1;
  $empty_option = array(
    '_none' => theme('select_or_other_none', array(
      'instance' => $instance,
    )),
  );

  // Multiple select.
  if ($multiple) {

    // Add a 'none' option for non-required fields.
    if (!$required) {
      $options = $empty_option + $options;
    }
  }
  elseif (!$multiple_checkbox) {

    // Add a 'none' option for non-required fields, and a 'select a value'
    // option for required fields that do not come with a value selected.
    if (!$required) {
      $options = $empty_option + $options;
    }
  }

  // @todo: This isset() can probably be taken out in drupal 8.
  if (isset($settings['sort_options']) && $settings['sort_options']) {
    natcasesort($options);
  }
  return $options;
}