You are here

function select_with_style_field_widget_form in Select with Style 7

Implements hook_field_widget_form().

Called by core when a user selects either the 'select_optgroups' or 'select_tree' or widgets. Also when the user edits a node. A hook_form_alter() implementation isn't going to cut it, as the select_tree or select_optgroups field won't be on the form without this function!

File

select_with_style/select_with_style.module, line 188
select_with_style.module

Code

function select_with_style_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  $required = $element['#required'];
  $value_key = key($field['columns']);
  $has_value = isset($items[0][$value_key]);
  $properties = _options_properties('select', $multiple, $required, $has_value);

  // Prepare the list of options and their attributes.
  // Similar to _options_get_options(), but also returns option attributes.
  list($options, $option_attributes) = _select_with_style_get_options_and_attributes($field, $instance, $properties);
  $properties['optgroups'] = TRUE;

  // prevents flattening of $options.
  $default_value = _options_storage_to_form($items, $options, $value_key, $properties);
  $settings = $instance['widget']['settings']['select_with_style'];
  $element += array(
    '#type' => 'select',
    '#multiple' => $multiple && count($options) > 1,
    '#options' => $options,
    '#option_attributes' => $option_attributes,
    '#default_value' => $default_value,
    '#value_key' => $value_key,
    '#element_validate' => array(
      'options_field_widget_validate',
    ),
    '#properties' => $properties,
  );
  if ($multiple && !empty($settings['multi_select_height'])) {
    $element['#size'] = $settings['multi_select_height'];
  }

  // Add core and optional CSS
  $element['#attached']['css'][] = drupal_get_path('module', 'select_with_style') . '/select_with_style.css';
  if (!empty($settings['css file'])) {
    if (!is_array($settings['css file'])) {
      $settings['css file'] = array(
        $settings['css file'],
      );
    }
    $css_files = select_with_style_css_files();
    foreach ($settings['css file'] as $css_file) {
      if (!empty($css_files[$css_file])) {
        $element['#attached']['css'][] = $css_files[$css_file];
      }
    }
  }
  return $element;
}