You are here

protected function TermReference::valueForm in EntityFieldQuery Views Backend 8

Options form subform for setting options.

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

Overrides InOperator::valueForm

See also

buildOptionsForm()

File

src/Plugin/views/filter/TermReference.php, line 100
Contains \Drupal\efq_views\Plugin\views\filter\FieldInOperator.

Class

TermReference
Filter by term id.

Namespace

Drupal\efq_views\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
  $vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
  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) {
      $query = new EntityFieldQuery();
      $result = $query
        ->entityCondition('entity_type', 'taxonomy_term')
        ->entityCondition('entity_id', $this->value)
        ->execute();
      if (!empty($result['taxonomy_term'])) {
        $terms = entity_load('taxonomy_term', array_keys($result['taxonomy_term']));
        foreach ($terms as $term) {
          if ($default) {
            $default .= ', ';
          }
          $default .= $term->name;
        }
      }
    }
    $form['value'] = array(
      '#title' => t('Select terms from vocabulary @voc', array(
        '@voc' => $vocabulary->name,
      )),
      '#type' => 'textfield',
      '#default_value' => $default,
    );
    if ($this->options['limit']) {
      $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
    }
  }
  else {
    $options = array();
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'taxonomy_term');
    if ($this->options['limit']) {
      $query
        ->propertyCondition('vid', (int) $vocabulary->vid);
    }
    $result = $query
      ->execute();
    if (!empty($result['taxonomy_term'])) {
      $terms = entity_load('taxonomy_term', array_keys($result['taxonomy_term']));
      foreach ($terms 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'])) {
        $options = $this
          ->reduce_valueOptions($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);
        }
        else {
          $copy = $default_value;
          $default_value = array_shift($copy);
        }
      }
    }
    $form['value'] = array(
      '#type' => 'select',
      '#title' => $this->options['limit'] ? t('Select terms from vocabulary @voc', array(
        '@voc' => $vocabulary->name,
      )) : t('Select terms'),
      '#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;
    }
  }
}