Views autocomplete API | Drupal 8 Project


Project Name : Views autocomplete API
Drupal.org project Link : Views autocomplete API
Created : 2015-06-02
Last Update : 2017-01-03
Current Version : 8.x-1.0-beta1
Download Views autocomplete API 8.x-1.0-beta1 Version From Drupal.org : Download

This module allows you to populate any autocomplete textfield via Views driven data and as little coding as possible. This is a tiny module. It is known to work with views built on top of Search API module.

This module does not package any style for now. Screenshot is an exemple of result you can achieve after some CSS work. Of course, we would be happy to include any provided style proposed in the issue queue !

********** Drupal 8 **********

1 - Create one or more views :

  • The last field of the View is used to show the proposed results. You can rewrite results to combine multiple fields (images, links, texts, etc.)
  • The field before will be used for injection on selection and should be plain text (you can rewrite the field as well)
  • You can use header / footer and no result behavior.
  • The exposed filters will be used to filter the view with the input of the user into the autocomplete text field (you can user AND / OR, so for example seach on users the name or email). You can still use non-exposed filters to restrict the search.
  • You can use [autocomplete] token that contains the current search input

2 - Just do a little form_alter against any textfield element :

$form['my_text_field']['#autocomplete_route_name'] = 'views_autocomplete_api'; $form['my_text_field']['#autocomplete_route_parameters'] = array('view_name' => 'my_view_name1,my_view_name2,...'); 

Note that the master display of the Views will be used.

Views Autocomplete Search is a good example for testing some of thoses functionnalities applied to the search field of the core default search block.

Differences with the D7 version : you can use [autocomplete] token wherever you are (for example in the header of the View to link to a research page). Drupal 8 version supports highlighting as well.

********** Drupal 7 **********

1 - Create one or more View(s) :
> The last field of the view is used to show the autocomplete options to the user.
> The previous one is used to define what will be sent to the form when
selecting the option.
> The exposed input filters will be filled with the user input. You can use OR / AND.
You can use the header of the views for permanent elements, and pagination to define how many items you want to show and define the access.

2 - On any textfield form element, just do a form alter to have :

"#autocomplete_path" => 'views_autocomplete_api/[the machine name of your view 1]/[the machine name of your view 2]/...'

Note that the master display of the Views will be used (you can show it in the advanced options of Views).

3 - If needed, you can always alter the result set by using hook_autocomplete_api_alter and add, modify or remove results.

Similar module :
Search Autocomplete : uses Views as well to generate autocomplete path.