You are here

function range_field_widget_form in Range 7

Implements hook_field_widget_form().

File

./range.module, line 622
Defines range field types.

Code

function range_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $instance = range_translate_field_instance($instance);
  $settings = $instance['settings'];
  $from = isset($items[$delta]['from']) ? $items[$delta]['from'] : '';
  $to = isset($items[$delta]['to']) ? $items[$delta]['to'] : '';

  // Substitute the decimal separator.
  if ($field['type'] === 'range_decimal' || $field['type'] === 'range_float') {
    $from = strtr($from, '.', $field['settings']['decimal_separator']);
    $to = strtr($to, '.', $field['settings']['decimal_separator']);
  }

  // Wrap in a fieldset for single field.
  if ($field['cardinality'] == 1) {
    $element['#type'] = 'fieldset';
  }
  $element += array(
    // Extract the range type from the field type name for easier validation.
    '#range_type' => str_replace('range_', '', $field['type']),
  );
  $base = array(
    '#type' => 'textfield',
    // Allow a slightly larger size that the field length to allow for some
    // configurations where all characters won't fit in input field.
    '#size' => $field['type'] === 'range_decimal' ? $field['settings']['precision'] + 4 : 12,
    // Allow two extra characters for signed values and decimal separator.
    '#maxlength' => $field['type'] === 'range_decimal' ? $field['settings']['precision'] + 2 : 10,
    '#required' => $element['#required'],
  );
  $element['from'] = array(
    '#title' => $settings['from']['label'],
    '#default_value' => $from,
    '#prefix' => '<div class="field-values-wrapper clearfix">',
  ) + $base;
  $element['to'] = array(
    '#title' => $settings['to']['label'],
    '#default_value' => $to,
    '#suffix' => '</div>',
  ) + $base;

  // Add prefixes and suffixes.
  if (!empty($settings['field']['prefix'])) {
    $settings['from']['prefix'] = $settings['field']['prefix'] . $settings['from']['prefix'];
  }
  if (!empty($settings['field']['suffix'])) {
    $settings['to']['suffix'] .= $settings['field']['suffix'];
  }
  _range_field_widget_form_subelement_prefix_suffix($element, 'from', $settings);
  _range_field_widget_form_subelement_prefix_suffix($element, 'to', $settings);
  $element['#element_validate'][] = 'range_field_widget_validate';
  $element['#attached']['css'][] = drupal_get_path('module', 'range') . '/css/range.admin.css';
  return $element;
}