You are here

public function SearchApiViewsHandlerFilterTaxonomyTerm::value_form in Search API 7

Provide a form for setting the filter value.

Overrides SearchApiViewsHandlerFilterEntity::value_form

File

contrib/search_api_views/includes/handler_filter_taxonomy_term.inc, line 58
Contains SearchApiViewsHandlerFilterTaxonomyTerm.

Class

SearchApiViewsHandlerFilterTaxonomyTerm
Views filter handler class for taxonomy term entities.

Code

public function value_form(&$form, &$form_state) {
  parent::value_form($form, $form_state);
  if (!empty($this->definition['vocabulary'])) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($this->definition['vocabulary']);
    $title = t('Select terms from vocabulary @voc', array(
      '@voc' => $vocabulary->name,
    ));
  }
  else {
    $vocabulary = FALSE;
    $title = t('Select terms');
  }
  $form['value']['#title'] = $title;
  if ($vocabulary && $this->options['type'] == 'textfield') {
    $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
  }
  else {
    if ($vocabulary && !empty($this->options['hierarchy'])) {
      $tree = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
      $options = array();
      if ($tree) {
        foreach ($tree as $term) {
          $choice = new stdClass();
          $choice->option = array(
            $term->tid => str_repeat('-', $term->depth) . check_plain(entity_label('taxonomy_term', $term)),
          );
          $options[] = $choice;
        }
      }
    }
    else {
      $options = array();
      $query = db_select('taxonomy_term_data', 'td');
      $query
        ->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
      $query
        ->fields('td');
      $query
        ->orderby('tv.weight');
      $query
        ->orderby('tv.name');
      $query
        ->orderby('td.weight');
      $query
        ->orderby('td.name');
      $query
        ->addTag('taxonomy_term_access');
      if ($vocabulary) {
        $query
          ->condition('tv.machine_name', $vocabulary->machine_name);
      }
      $result = $query
        ->execute();
      $tids = array();
      foreach ($result as $term) {
        $tids[] = $term->tid;
      }
      $terms = taxonomy_term_load_multiple($tids);
      foreach ($terms as $term) {
        $options[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
      }
    }
    $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']['#type'] = 'select';
    $form['value']['#multiple'] = TRUE;
    $form['value']['#options'] = $options;
    $form['value']['#size'] = min(9, count($options));
    $form['value']['#default_value'] = $default_value;
    if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
      $form_state['input'][$identifier] = $default_value;
    }
  }
}