You are here

public function ViewsAutocompleteFiltersTrait::valueForm in Views Autocomplete Filters 8

File

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

Class

ViewsAutocompleteFiltersTrait
Provides common methods for all Views Autocomplete Filters.

Namespace

Drupal\views_autocomplete_filters\Plugin\views\filter

Code

public function valueForm(&$form, FormStateInterface $form_state) {
  parent::valueForm($form, $form_state);
  $exposed = $form_state
    ->get('exposed');
  if (!$exposed || empty($this->options['expose']['autocomplete_filter'])) {

    // It is not an exposed form or autocomplete is not enabled.
    return;
  }
  if (empty($form['value']['#type']) || $form['value']['#type'] !== 'textfield') {

    // Not a textfield.
    return;
  }

  // Add autocomplete path to the exposed textfield.
  $view_args = !empty($this->view->args) ? implode('||', $this->view->args) : 0;
  $form['value']['#autocomplete_route_name'] = 'viewsfilters.autocomplete';
  $form['value']['#autocomplete_route_parameters'] = [
    'view_name' => $this->view->storage
      ->get('id'),
    'view_display' => $this->view->current_display,
    'filter_name' => $this->options['id'],
    'view_args' => $view_args,
  ];

  // Add JS script to expands the behaviour of the default autocompletion.
  // Override the "select" option of the jQueryUI auto-complete for
  // to make sure we do not use quotes for inputs with comma.
  $form['#attached']['library'][] = 'views_autocomplete_filters/drupal.views-autocomplete-filters';

  // Add JS script with core autocomplete overrides to the end of JS files
  // list to be sure it is added after the "misc/autocomplete.js" file. Also
  // mark the field with special class.
  if (!empty($this->options['expose']['autocomplete_dependent'])) {
    $form['#attached']['library'][] = 'views_autocomplete_filters/drupal.views-autocomplete-filters-dependent';
    $form['value']['#attributes']['class'][] = 'views-ac-dependent-filter';
  }
}