Advertising sustains the DA. Ads are hidden for members. Join today

Using custom autocomplete scripts

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

This module offers a way to use a custom script for creating the autocomplete suggestions instead of the built-in menu handler. This allows creating autocomplete suggestions without first bootstrapping Drupal, thus greatly improving performance. Just keep in mind that this will, by necessity, circumvent all access checks for the suggestions, so make sure this won't lead to any security leaks on your site.

How to set up

You need to know:

  • The machine name of the autocomplete search – can be seen as the last-but-one path segment when editing the autocomplete settings for the search (path: admin/config/search/search_api/index/INDEX_ID/autocomplete/AUTOCOMPLETE_SEARCH_ID/edit).
  • The path/URL of your custom script – which is just the path of the script file relative to the root of your Drupal installation. You can put the script file anywhere you want inside the installation, as long as it's accessible from the web.

Then, just set the value of the search_api_autocomplete_scripts variable to the following value:

array('AUTOCOMPLETE_SEARCH_ID' => 'SCRIPT_PATH')

And that's it. The autocomplete script will get two GET parameters, search with the user input and machine_name with the autocomplete search's machine name (in case you want to use the same script for more than one search).
You can also create the script path dynamically (e.g., to include the current language – but don't use for something like user privileges, as the path can easily be changed by the user!) by using array('#callback' => 'function_name') instead of 'SCRIPT_PATH' in the variable value – see callback_search_api_autocomplete_script_url() for the expected function signature.

Example script

In the project's Git repository, you can find an example for a custom autocomplete script, using a Solr server (and more or less the same code as the Search API Solr Search module).

Help improve this page

Page status: Not set

You can: