August 8, 2011

Drupal: Overriding Drupal autocompletion to pass more parameters

August 8, 2011 - Categories: drupal, geek
Update 2014-02-19: See LittleDynamo’s comment with a link to this StackOverflow answer.
Update 2014-02-11: See Claus’ comment below for a better way to do this.

Drupal autocompletion is easy – just add #autocomplete_path to a Form API element, set it to something that returns a JSON hash, and off you go.

What if you want to pass form values into your autocompletion function so that you can filter results?

Searching, I found some pages that suggested changing the value in the hidden autocomplete field so that it would go to a different URL. However, that probably doesn’t handle the autocomplete cache. Here’s another way to do it:

Drupal.ACDB.prototype.customSearch = function (searchString) {
    searchString = searchString + "/" + $("#otherfield").val();

Drupal.jsAC.prototype.populatePopup = function () {
  // Show popup
  if (this.popup) {
  this.selected = false;
  this.popup = document.createElement('div'); = 'autocomplete';
  this.popup.owner = this;
    marginTop: this.input.offsetHeight +'px',
    width: (this.input.offsetWidth - 4) +'px',
    display: 'none'

  // Do search
  this.db.owner = this;
  if ( == 'edit-your-search-field') {
  } else {;

Drupal.behaviors.rebindAutocomplete = function(context) {
    // Unbind the behaviors to prevent multiple search handlers
    // Rebind autocompletion with the new code

You’ll need to use drupal_add_js to add misc/autocomplete.js before you add the Javascript file for your form.

Hope this helps!

2011-08-08 Mon 19:16