You are here

function rooms_booking_handler_unit_filter::value_form in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Options form subform for setting options.

This should be overridden by all child classes and it must define $form['value'].

Overrides views_handler_filter_many_to_one::value_form

See also

options_form()

File

modules/rooms_booking/views/rooms_booking_handler_unit_filter.inc, line 33

Class

rooms_booking_handler_unit_filter
@file

Code

function value_form(&$form, &$form_state) {
  if ($this->options['type'] == 'textfield') {
    $default = '';
    if ($this->value) {
      $result = rooms_unit_load_multiple($this->value);
      foreach ($result as $entity) {
        if ($default) {
          $default .= ', ';
        }
        $default .= entity_label('rooms_unit', $entity);
      }
    }
    $form['value'] = array(
      '#title' => t('Select units'),
      '#type' => 'textfield',
      '#default_value' => $default,
      '#autocomplete_path' => 'admin/views/ajax/autocomplete/rooms_unit',
    );
  }
  else {
    $options = array();
    $query = db_select('rooms_units', 't');
    $query
      ->fields('t');
    $query
      ->orderby('t.name');
    $result = $query
      ->execute();
    $unit_ids = array();
    foreach ($result as $unit) {
      $unit_ids[] = $unit->unit_id;
    }
    $entities = rooms_unit_load_multiple($unit_ids);
    foreach ($entities as $entity) {
      $options[$entity->unit_id] = entity_label('rooms_unit', $entity);
    }
    $default_value = (array) $this->value;
    if (!empty($form_state['exposed'])) {
      $identifier = $this->options['expose']['identifier'];
      if (!empty($this->options['expose']['reduce'])) {
        $options = $this
          ->reduce_value_options($options);
        if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
          $default_value = array();
        }
      }
      if (empty($this->options['expose']['multiple'])) {
        if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
          $default_value = 'All';
        }
        elseif (empty($default_value)) {
          $keys = array_keys($options);
          $default_value = array_shift($keys);
        }
        elseif ($default_value == array(
          '',
        )) {
          $default_value = 'All';
        }
        else {
          $copy = $default_value;
          $default_value = array_shift($copy);
        }
      }
    }
    $form['value'] = array(
      '#type' => 'select',
      '#title' => t('Select units'),
      '#multiple' => TRUE,
      '#options' => $options,
      '#size' => min(9, count($options)),
      '#default_value' => $default_value,
    );
  }
}