You are here

function slide_with_style_field_widget_form in Select with Style 7

Implements hook_field_widget_form().

Creates a textfield with CSS class for the JS to attach the slider to.

1 call to slide_with_style_field_widget_form()
slide_with_style_handler_filter_numeric::value_form in slide_with_style/views/slide_with_style_handler_filter_numeric.inc
Provide a simple textfield for equality

File

slide_with_style/slide_with_style.module, line 74
Defines a slider RANGE widget to create filters for numeric and list fields.

Code

function slide_with_style_field_widget_form(&$form, &$form_state, $field, $instance, $lang, $items, $delta, $element) {
  switch ($field['module']) {
    case 'list':
      $additions = array(
        'value' => array(
          '#title' => $instance['label'],
          '#type' => 'textfield',
          '#default_value' => isset($items[$delta]) ? reset($items[$delta]) : NULL,
          '#required' => !empty($element['#required']),
          '#size' => 5,
        ),
      );
      break;
    default:

      // Use the default numeric field widget as a base.
      $additions = number_field_widget_form($form, $form_state, $field, $instance, $lang, $items, $delta, $element);

      // When used as a range, this field is read-only
      $additions['value']['#size'] = min(8, $additions['value']['#size']);
  }

  // All additions are in $element['value']
  $element += $additions;

  // Define the class through which the javascript will find this textfield.
  $element['value']['#attributes']['class'][] = "edit-slide-with-style-slider";

  // Append to the textfield a placeholder for the slider
  $element['value']['#suffix'] = '<div class="slide-with-style-slider"></div>';
  if (empty($form_state['slider_id'])) {
    $has_multiple_values = field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT;

    // id needs to match up with the element id generated by core for this input element.
    $parent_id = '';
    if (!empty($element['#field_parents'])) {
      $parent_id .= trim($element['#field_parents'][0], '_ ') . '-';
      if (!empty($element['#field_parents'][1])) {
        $parent_id .= trim($element['#field_parents'][1], '_ ') . '-';
      }
    }
    $base_id = 'edit-' . $parent_id . trim($instance['field_name'], '_ ') . "-{$lang}-";
    $id = $base_id . ($has_multiple_values ? "{$delta}-value" : 'value');
  }
  else {
    $id = $form_state['slider_id'];
  }

  // Pass jQueryUI slider parameters via the js settings.
  $widget_settings = $instance['widget']['settings'];
  $text_values = FALSE;
  if ($field['module'] == 'number') {
    $min = $instance['settings']['min'];
    $max = $instance['settings']['max'];
  }
  elseif ($field['module'] == 'list') {
    if (!empty($field['settings']['allowed_values'])) {
      $text_values = $field['settings']['allowed_values'];
      $keys = array_keys($text_values);
      $min = reset($keys);
      $max = array_pop($keys);
    }
  }

  // Do not use $element['value']['#default_value'] -- it is the same as $items,
  // but may be formatted.
  if (isset($items[$delta]['value'])) {
    $initial_value = $items[$delta]['value'];
  }
  elseif (isset($instance['default_value'][0])) {
    $initial_value = $instance['default_value'][0]['value'];
  }
  else {
    $initial_value = $min;
  }
  $slider_parameters = array(
    drupal_html_class($id) => array(
      'step' => empty($widget_settings['step']) ? 1 : $widget_settings['step'],
      'min' => $min,
      'max' => $max,
      'value' => $initial_value,
      'values' => empty($element['#default_values']) ? NULL : $element['#default_values'],
      'textfield' => !empty($widget_settings['appearance']['with_textfield']),
      'textvalues' => $text_values,
      'bubble' => !empty($widget_settings['appearance']['with_bubble']),
      'orientation' => empty($widget_settings['appearance']['vertical']) ? 'horizontal' : 'vertical',
      'range' => empty($element['#default_values']) ? 'min' : TRUE,
      'autosubmit' => !empty($element['#autosubmit']),
    ),
  );

  // Start by attaching settings or it will break global_filter_field_slider_widget_form_alter()
  $element['value']['#attached']['js'][] = array(
    'data' => array(
      'slider' => $slider_parameters,
    ),
    'type' => 'setting',
    'scope' => 'header',
  );
  $path = drupal_get_path('module', 'slide_with_style');
  $element['value']['#attached']['js'][] = $path . '/slide_with_style.js';
  $element['value']['#attached']['css'][] = $path . '/slide_with_style.css';
  $css_files = slide_with_style_css_files();
  if (!empty($css_files[$widget_settings['css file']])) {
    $element['value']['#attached']['css'][] = $css_files[$widget_settings['css file']];
  }
  $element['value']['#attached']['library'][] = array(
    'system',
    'ui.slider',
  );

  // Just in case we need to do more stuff after the form has been built...
  $element['value']['#process'][] = 'slide_with_style_field_widget_process';
  return $element;
}