1 views_handler_filter_numeric.inc views_handler_filter_numeric::value_form(&$form, &$form_state)

Provide a simple textfield for equality

Overrides views_handler_filter::value_form

File

core/modules/views/handlers/views_handler_filter_numeric.inc, line 138
Definition of views_handler_filter_numeric.

Class

views_handler_filter_numeric
Simple filter to handle greater than/less than filters

Code

function value_form(&$form, &$form_state) {
  $form['value']['#tree'] = TRUE;

  // We have to make some choices when creating this as an exposed
  // filter form. For example, if the operator is locked and thus
  // not rendered, we can't render dependencies; instead we only
  // render the form items we need.
  $which = 'all';
  if (!empty($form['operator'])) {
    $source = ':input[name="options[operator]"]';
  }

  if (!empty($form_state['exposed'])) {
    $identifier = $this->options['expose']['identifier'];

    if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
      // exposed and locked.
      $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
    }
    else {
      $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
    }
  }

  if ($which == 'all') {
    $form['value']['value'] = array(
      '#type' => 'textfield',
      '#title' => empty($form_state['exposed']) ? t('Value') : '',
      '#size' => 30,
      '#default_value' => $this->value['value'],
    );
    // Setup #states for all operators with one value.
    if (isset($source)) {
      foreach ($this->operator_values(1) as $operator) {
        $form['value']['value']['#states']['visible'][] = array(
          $source => array('value' => $operator),
        );
      }
    }
    if (isset($identifier) && !empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
      $form_state['input'][$identifier]['value'] = $this->value['value'];
    }
  }
  elseif ($which == 'value') {
    // When exposed we drop the value-value and just do value if
    // the operator is locked.
    $form['value'] = array(
      '#type' => 'textfield',
      '#title' => empty($form_state['exposed']) ? t('Value') : '',
      '#size' => 30,
      '#default_value' => $this->value['value'],
    );
    if (isset($identifier) && !empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
      $form_state['input'][$identifier] = $this->value['value'];
    }
  }

  if ($which == 'all' || $which == 'minmax') {
    $form['value']['min'] = array(
      '#type' => 'textfield',
      '#title' => empty($form_state['exposed']) ? t('Min') : '',
      '#size' => 30,
      '#default_value' => $this->value['min'],
    );
    $form['value']['max'] = array(
      '#type' => 'textfield',
      '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
      '#size' => 30,
      '#default_value' => $this->value['max'],
    );
    if ($which == 'all') {
      $states = array();
      // Setup #states for all operators with two values.
      if (isset($source)) {
        foreach ($this->operator_values(2) as $operator) {
          $states['#states']['visible'][] = array(
            $source => array('value' => $operator),
          );
        }
      }
      $form['value']['min'] += $states;
      $form['value']['max'] += $states;
    }
    if (isset($identifier) && !empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
      $form_state['input'][$identifier]['min'] = $this->value['min'];
    }
    if (isset($identifier) && !empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
      $form_state['input'][$identifier]['max'] = $this->value['max'];
    }

    if (!isset($form['value'])) {
      // Ensure there is something in the 'value'.
      $form['value'] = array(
        '#type' => 'value',
        '#value' => NULL
      );
    }
  }
}