You are here

public function ViewsAutocompleteFiltersTrait::buildOptionsForm in Views Autocomplete Filters 8

Build the options form.

File

src/Plugin/views/filter/ViewsAutocompleteFiltersTrait.php, line 36

Class

ViewsAutocompleteFiltersTrait
Provides common methods for all Views Autocomplete Filters.

Namespace

Drupal\views_autocomplete_filters\Plugin\views\filter

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  if (!$this
    ->canExpose() || empty($form['expose'])) {
    return;
  }
  $field_options = $this
    ->getFieldOptions();
  if (empty($this->options['expose']['autocomplete_field']) && !empty($field_options[$this->options['id']])) {
    $this->options['expose']['autocomplete_field'] = $this->options['id'];
  }

  // Build form elements for the right side of the exposed filter form.
  $states = [
    'visible' => [
      '
          :input[name="options[expose][autocomplete_filter]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $form['expose'] += [
    'autocomplete_filter' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Autocomplete'),
      '#default_value' => $this->options['expose']['autocomplete_filter'],
      '#description' => $this
        ->t('Use Autocomplete for this filter.'),
    ],
    'autocomplete_items' => [
      '#type' => 'number',
      '#title' => $this
        ->t('Maximum number of items in Autocomplete'),
      '#default_value' => $this->options['expose']['autocomplete_items'],
      '#description' => $this
        ->t('Enter 0 for no limit.'),
      '#min' => 0,
      '#states' => $states,
    ],
    'autocomplete_min_chars' => [
      '#type' => 'number',
      '#title' => $this
        ->t('Minimum number of characters to start filter'),
      '#default_value' => $this->options['expose']['autocomplete_min_chars'],
      '#min' => 0,
      '#states' => $states,
    ],
    'autocomplete_dependent' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Suggestions depend on other filter fields'),
      '#default_value' => $this->options['expose']['autocomplete_dependent'],
      '#description' => $this
        ->t('Autocomplete suggestions will be filtered by other filter fields'),
      '#states' => $states,
    ],
    'autocomplete_field' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Field with autocomplete results'),
      '#default_value' => $this->options['expose']['autocomplete_field'],
      '#options' => $field_options,
      '#description' => $this
        ->t('Selected field will be used for dropdown results of autocomplete. In most cases it should be the same field you use for filter.'),
      '#states' => $states,
    ],
    'autocomplete_raw_dropdown' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Unformatted dropdown'),
      '#default_value' => $this->options['expose']['autocomplete_raw_dropdown'],
      '#description' => $this
        ->t('Use unformatted data from database for dropdown list instead of field formatter result. Value will be printed as plain text.'),
      '#states' => $states,
    ],
    'autocomplete_raw_suggestion' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Unformatted suggestion'),
      '#default_value' => $this->options['expose']['autocomplete_raw_suggestion'],
      '#description' => $this
        ->t('The same as above, but for suggestion (text appearing inside textfield when item is selected).'),
      '#states' => $states,
    ],
  ];
  if (!$this
    ->hasAutocompleteFieldSelector()) {
    unset($form['expose']['autocomplete_field']);
  }
}