TaxonomyIndexTid.php 11.3 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Definition of views_handler_filter_term_node_tid.
 */

8
namespace Drupal\taxonomy\Plugin\views\filter;
9

10
use Drupal\views\ViewExecutable;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\Component\Annotation\PluginID;
13 14
use Drupal\views\Plugin\views\filter\ManyToOne;

15 16 17 18
/**
 * Filter by term id.
 *
 * @ingroup views_filter_handlers
19
 *
20
 * @PluginID("taxonomy_index_tid")
21 22
 */
class TaxonomyIndexTid extends ManyToOne {
23

24 25 26
  // Stores the exposed input for this filter.
  var $validated_exposed_input = NULL;

27 28 29 30 31 32
  /**
   * Overrides \Drupal\views\Plugin\views\filter\ManyToOne::init().
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

33
    if (!empty($this->definition['vocabulary'])) {
34
      $this->options['vid'] = $this->definition['vocabulary'];
35 36 37
    }
  }

38
  public function hasExtraOptions() { return TRUE; }
39

40
  public function getValueOptions() { /* don't overwrite the value options */ }
41

42 43
  protected function defineOptions() {
    $options = parent::defineOptions();
44 45 46

    $options['type'] = array('default' => 'textfield');
    $options['limit'] = array('default' => TRUE, 'bool' => TRUE);
47
    $options['vid'] = array('default' => '');
48 49 50 51 52 53
    $options['hierarchy'] = array('default' => 0);
    $options['error_message'] = array('default' => TRUE, 'bool' => TRUE);

    return $options;
  }

54
  public function buildExtraOptionsForm(&$form, &$form_state) {
55
    $vocabularies = entity_load_multiple('taxonomy_vocabulary');
56 57
    $options = array();
    foreach ($vocabularies as $voc) {
58
      $options[$voc->id()] = $voc->label();
59 60 61 62
    }

    if ($this->options['limit']) {
      // We only do this when the form is displayed.
63
      if (empty($this->options['vid'])) {
64
        $first_vocabulary = reset($vocabularies);
65
        $this->options['vid'] = $first_vocabulary->id();
66 67 68
      }

      if (empty($this->definition['vocabulary'])) {
69
        $form['vid'] = array(
70 71 72 73
          '#type' => 'radios',
          '#title' => t('Vocabulary'),
          '#options' => $options,
          '#description' => t('Select which vocabulary to show terms for in the regular options.'),
74
          '#default_value' => $this->options['vid'],
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        );
      }
    }

    $form['type'] = array(
      '#type' => 'radios',
      '#title' => t('Selection type'),
      '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
      '#default_value' => $this->options['type'],
    );

    $form['hierarchy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show hierarchy in dropdown'),
      '#default_value' => !empty($this->options['hierarchy']),
      '#states' => array(
        'visible' => array(
          ':input[name="options[type]"]' => array('value' => 'select'),
        ),
      ),
    );
  }

98
  protected function valueForm(&$form, &$form_state) {
99
    $vocabulary = entity_load('taxonomy_vocabulary', $this->options['vid']);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    if (empty($vocabulary) && $this->options['limit']) {
      $form['markup'] = array(
        '#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
      );
      return;
    }

    if ($this->options['type'] == 'textfield') {
      $default = '';
      if ($this->value) {
        $result = db_select('taxonomy_term_data', 'td')
          ->fields('td')
          ->condition('td.tid', $this->value)
          ->execute();
        foreach ($result as $term) {
          if ($default) {
            $default .= ', ';
          }
          $default .= $term->name;
        }
      }

      $form['value'] = array(
123
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->label())) : t('Select terms'),
124 125 126 127 128
        '#type' => 'textfield',
        '#default_value' => $default,
      );

      if ($this->options['limit']) {
129
        $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->id();
130 131 132 133
      }
    }
    else {
      if (!empty($this->options['hierarchy']) && $this->options['limit']) {
134
        $tree = taxonomy_get_tree($vocabulary->id());
135 136 137 138 139 140 141 142 143 144 145 146 147 148
        $options = array();

        if ($tree) {
          foreach ($tree as $term) {
            $choice = new stdClass();
            $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
            $options[] = $choice;
          }
        }
      }
      else {
        $options = array();
        $query = db_select('taxonomy_term_data', 'td');
        $query->fields('td');
149
        // @todo Sorting on vocabulary properties http://drupal.org/node/1821274
150 151 152 153
        $query->orderby('td.weight');
        $query->orderby('td.name');
        $query->addTag('term_access');
        if ($this->options['limit']) {
154
          $query->condition('td.vid', $vocabulary->id());
155 156 157 158 159 160 161 162 163 164 165 166 167
        }
        $result = $query->execute();
        foreach ($result as $term) {
          $options[$term->tid] = $term->name;
        }
      }

      $default_value = (array) $this->value;

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

        if (!empty($this->options['expose']['reduce'])) {
168
          $options = $this->reduceValueOptions($options);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

          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);
          }
          // Due to #1464174 there is a chance that array('') was saved in the admin ui.
          // Let's choose a safe default value.
          elseif ($default_value == array('')) {
            $default_value = 'All';
          }
          else {
            $copy = $default_value;
            $default_value = array_shift($copy);
          }
        }
      }
      $form['value'] = array(
        '#type' => 'select',
196
        '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->label())) : t('Select terms'),
197 198 199 200 201 202 203 204 205 206 207 208 209
        '#multiple' => TRUE,
        '#options' => $options,
        '#size' => min(9, count($options)),
        '#default_value' => $default_value,
      );

      if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
        $form_state['input'][$identifier] = $default_value;
      }
    }

    if (empty($form_state['exposed'])) {
      // Retain the helper option
210
      $this->helper->buildOptionsForm($form, $form_state);
211 212 213
    }
  }

214
  protected function valueValidate($form, &$form_state) {
215 216 217 218 219 220 221 222 223 224 225 226 227
    // We only validate if they've chosen the text field style.
    if ($this->options['type'] != 'textfield') {
      return;
    }

    $values = drupal_explode_tags($form_state['values']['options']['value']);
    $tids = $this->validate_term_strings($form['value'], $values);

    if ($tids) {
      $form_state['values']['options']['value'] = $tids;
    }
  }

228
  public function acceptExposedInput($input) {
229 230 231 232 233 234
    if (empty($this->options['exposed'])) {
      return TRUE;
    }

    // If view is an attachment and is inheriting exposed filters, then assume
    // exposed input has already been validated
235
    if (!empty($this->view->is_attachment) && $this->view->display_handler->usesExposed()) {
236 237 238 239 240 241 242 243
      $this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
    }

    // If it's non-required and there's no value don't bother filtering.
    if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
      return FALSE;
    }

244
    $rc = parent::acceptExposedInput($input);
245 246 247 248 249 250 251 252 253 254
    if ($rc) {
      // If we have previously validated input, override.
      if (isset($this->validated_exposed_input)) {
        $this->value = $this->validated_exposed_input;
      }
    }

    return $rc;
  }

255
  public function validateExposed(&$form, &$form_state) {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    if (empty($this->options['exposed'])) {
      return;
    }

    $identifier = $this->options['expose']['identifier'];

    // We only validate if they've chosen the text field style.
    if ($this->options['type'] != 'textfield') {
      if ($form_state['values'][$identifier] != 'All')  {
        $this->validated_exposed_input = (array) $form_state['values'][$identifier];
      }
      return;
    }

    if (empty($this->options['expose']['identifier'])) {
      return;
    }

    $values = drupal_explode_tags($form_state['values'][$identifier]);

    $tids = $this->validate_term_strings($form[$identifier], $values);
    if ($tids) {
      $this->validated_exposed_input = $tids;
    }
  }

  /**
   * Validate the user string. Since this can come from either the form
   * or the exposed filter, this is abstracted out a bit so it can
   * handle the multiple input sources.
   *
   * @param $form
   *   The form which is used, either the views ui or the exposed filters.
   * @param $values
   *   The taxonomy names which will be converted to tids.
   *
   * @return array
   *   The taxonomy ids fo all validated terms.
   */
  function validate_term_strings(&$form, $values) {
    if (empty($values)) {
      return array();
    }

    $tids = array();
    $names = array();
    $missing = array();
    foreach ($values as $value) {
      $missing[strtolower($value)] = TRUE;
      $names[] = $value;
    }

    if (!$names) {
      return FALSE;
    }

    $query = db_select('taxonomy_term_data', 'td');
    $query->fields('td');
    $query->condition('td.name', $names);
315
    $query->condition('td.vid', $this->options['vid']);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    $query->addTag('term_access');
    $result = $query->execute();
    foreach ($result as $term) {
      unset($missing[strtolower($term->name)]);
      $tids[] = $term->tid;
    }

    if ($missing && !empty($this->options['error_message'])) {
      form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
    }
    elseif ($missing && empty($this->options['error_message'])) {
      $tids = array(0);
    }

    return $tids;
  }

333
  protected function valueSubmit($form, &$form_state) {
334 335 336
    // prevent array_filter from messing up our arrays in parent submit.
  }

337 338
  public function buildExposeForm(&$form, &$form_state) {
    parent::buildExposeForm($form, $form_state);
339 340 341 342 343 344 345 346 347 348
    if ($this->options['type'] != 'select') {
      unset($form['expose']['reduce']);
    }
    $form['error_message'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display error message'),
      '#default_value' => !empty($this->options['error_message']),
    );
  }

349
  public function adminSummary() {
350 351 352 353 354 355 356 357 358 359 360 361 362
    // set up $this->value_options for the parent summary
    $this->value_options = array();

    if ($this->value) {
      $this->value = array_filter($this->value);
      $result = db_select('taxonomy_term_data', 'td')
        ->fields('td')
        ->condition('td.tid', $this->value)
        ->execute();
      foreach ($result as $term) {
        $this->value_options[$term->tid] = $term->name;
      }
    }
363
    return parent::adminSummary();
364
  }
365

366
}