I have made textfield to be autocomplete field by using hook_form_alter.

mymoudle_form_alter(&$form, &$form_state, $form_id){
   $form['field_state']['und'][0]['value']['#autocomplete_path'] ='get-state-values';
    function mymoudle_drupal_menu() {
    $items = array();   

    $items['get-country-value'] = array(
            'title' => 'Country Value',
            'page callback' => '_get_country_value',
            //'page arguments'  => array(1),
            'access arguments' => array('access content'),
            'type' => MENU_CALLBACK,
    $items['get-state-values'] = array(
            'page callback' => '_get_state_values',
            'access arguments' => array('access content'),
            'type' => MENU_CALLBACK
    return $items;

function _get_state_values($string){
    $country =$_SESSION['country']; 
    if($country == 'India'){                
        $state = array('Tamilnadu' => 'Tamilnadu','Karanataka' => 'Karanataka');
        $state = array('NewYork' => 'NewYork','Manahattan' => 'Manahattan');
    print drupal_json_output($state);exit;  
function _get_country_value(){
    $country = $_REQUEST['valueSelected'];  
    $_SESSION['country'] = $country;
    print drupal_json_output($country);exit;

If I select country as India and type in the state as 'n' it shows me in auto-complete as two states with n words like tamilnadu and karnataka which is correct(it runs ajax to fetch the values as shown in screenshot-1). enter image description here

The issue over here is if I again change the country to 'Us' and type same 'n' it shows same set of results and it doesn't run the ajax(as shown in screenshoot-2). Its showing from cache I think for previous results.

enter image description here

NOTE: I want an fix for the ajax caching issue.

I have attached the screenshot of what's happening .How to avoid this issue?.Any help will be much appreciated.Thanks in advance.

  • are the country and the states selects dependent on each other?how? what would you expect? – Pan Chrono Feb 10 '15 at 23:08
  • @PanChrono: Yes they are dependent country and states.If I select Us as country and try in state as 'U' it should list only US related states alone in autocomplete. – Ram Feb 11 '15 at 4:29
  • can you share the code for that dependency? – Pan Chrono Feb 11 '15 at 9:35
  • @PanChrono: I have shared the code. – Ram Feb 11 '15 at 9:50
  • @Ram: Have you tried this? drupal.org/node/2374709 – pown Feb 17 '15 at 5:07

Since, there is no proper way, I opted for hack (or work around) to get this working. As already mentioned by @Clive, removing/disabling caching mechanism would require more work. This solution just skips the caching part.

Step1: Copy the entire misc/autocomplete.js to your module js scripts directory.

Step2: Comment or remove the following line from the copied file

// See if this key has been searched for before.
  //if (this.cache[searchString]) {
      // We dont want some things to be cached
    //return this.owner.found(this.cache[searchString]);

Step3: Implement hook_js_alter to override it to specific pages you like.

 * Implements hook_js_alter
 * @param array $javascript
function MYMODULE_js_alter(&$javascript) {
  if (arg(0) == 'MYPAGE') {
    $javascript['misc/autocomplete.js']['data'] = drupal_get_path('module', 'MYMODULE') . '/js/autocomplete.js';
| improve this answer | |
  • 1
    I was looking for some-other way to do it instead of hacking. Since I can't find any other solutions for it I am using this method. – Ram Feb 23 '15 at 10:12
  • 2
    Same here, works also in Drupal 8.3, just look for all the places where the autocomplete.cache is used and comment those out, also the path is 'core/misc/autocomplete.js'! (you have to add the core/ part). – Larzan Aug 19 '17 at 13:15

Static caching is built in so you'll need to override Drupal.ACDB.proptotype.search.

This is untested but should be near the mark:

Drupal.ACDB.prototype.search = function (searchString) {
  var db = this;
  this.searchString = searchString;

  // See if this string needs to be searched for anyway.
  searchString = searchString.replace(/^\s+|\s+$/, '');
  if (searchString.length <= 0 ||
    searchString.charAt(searchString.length - 1) == ',') {

  // See if this key has been searched for before.
  //if (this.cache[searchString]) {
    //return this.owner.found(this.cache[searchString]);

  // Initiate delayed search.
  if (this.timer) {
  this.timer = setTimeout(function () {

    // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
    // encodeURIComponent to allow autocomplete search terms to contain slashes.
      type: 'GET',
      url: db.uri + '/' + Drupal.encodePath(searchString),
      dataType: 'json',
      success: function (matches) {
        if (typeof matches.status == 'undefined' || matches.status != 0) {
          db.cache[searchString] = matches;
          // Verify if these are still the matches the user wants to see.
          if (db.searchString == searchString) {
      error: function (xmlhttp) {
        alert(Drupal.ajaxError(xmlhttp, db.uri));
  }, this.delay);

That will change the behaviour site wide (or for the entire context in which you include the above snippet). To override for certain URIs you'll probably need to get a bit cleverer and monkey patch a few more core JS methods.

Also note that this doesn't disable caching, it just stops cached results from being returned instead of making the ajax call each time. To properly remove caching would require a bit more work.

| improve this answer | |
  • 1
    Nice workaround. This one will work, if the autocomplete.js is loaded before this script execution. – pown Feb 18 '15 at 4:42

The cache is saved in a property of ACDB object, unfortunately we can't access the instances of this object. However we can override the search function prototype object of Drupal.ACDB in order to delete this cache before every query.

Overriding Drupal.ACDB prototype will affect all autocompletes in Drupal, so you can minimize the impact (of not having cache) including your override javascript code only in specific pages. Also you can minimize the impact checking for the autocomplete ID or specific class or attribute since (inside the search function) you know which is the input in your DOM.

First you must backup the Drupal.ACDB.prototype.search function:

Drupal.ACDB.prototype.search_bkp = Drupal.ACDB.prototype.search;

Then you can override search function and apply your logic to erase the cache and then call search again.

Drupal.ACDB.prototype.search = 
    function (searchString) {  
        // your autocomplete element in DOM
        // use it like $(this.owner.input) to access jQuery functions
        console.log( 'AC =', this.owner.input ); 

        // Deleting the cache, apply your logic
        // ej. if ($(ac.owner.input).attr('id') === 'my-id-of-the-ac-input') {}
        this.cache = {};

Finally, you can execute the code above "whenever you want" (after Drupal.ACDB definition)... you can even try it in the Chrome console.

Hope that helps.

| improve this answer | |
  • This does not work for Drupal 8 anymore as ACDB has been removed: drupal.org/node/2202701 – Larzan Aug 19 '17 at 12:36
  • 1
    This works well. For consistency with the default ACDB object, it may be better to set cache to an empty object instead of an empty array: this.cache = {}; – David Lanier Sep 6 '17 at 15:35
  • @DavidLanier you are right. I changed it. – Beto Aveiga Sep 6 '17 at 15:46

I think your problem lies in how you are setting the session variable. Cache has nothing to do with it.

Drupal only needs to retrieve the values for the fields once to render the form on the server side. Your code doesn't throw any errors because the country field is rendered before the state field, so the state field uses the default country value (first item in the array) to perform its calculation.

You need some client-side code (JQuery) to refresh the state values via AJAX whenever the country value is changed.

Have a look at Tenken's answer to a very similar question.

I would also consider using a hierarchical taxonomy to store your values rather than hard-coding them as per this documentation page.

| improve this answer | |
  • Thanks,but this is not what I am expecting,I dont want to use hierarchical taxonomy since I am not following hierarchical pattern each one is different terms.I want an fix for the ajax caching issue. – Ram Feb 17 '15 at 4:50
  • Then ignore the last sentence and read the rest of the answer. In particular, follow the link to Tenken's answer. – Darvanen Feb 17 '15 at 4:53
  • AS I mentioned in the question I am making a textfield to be autocomplete using #autocomplete_path. $form['field_state']['und'][0]['value']['#autocomplete_path'] ='get-state-values';I cant pass an argument, by default it takes autocomplete search text as an argument. So that links doesn't help. – Ram Feb 17 '15 at 5:38

I think it is a session issue and session value only reflect after reload the page.

| improve this answer | |
  • 4
    Session isn't involved - there's a client-side cache of results based on the URL used to gather them, all handled in JS – Clive Feb 18 '15 at 11:31

I had a similar problem I created an entity reference field, and then I wanted to have custom autocomplete result list based on another field value (for example a select box). I managed to pass the select box value as filter parameter to the custom drupal autocomplete_path handler(at the backend). The problem if I searched for "a" and select "val1" from the select box I will get some result list,but if I changed the select box value to "val2" and type the same search character "a" I will get result list related to "val1". It will not send an ajax call again because it is cached. So after many days of search I found a solution that resolved my problem, and that is by overriding the "Drupal.ACDB.prototype.search" I found the solution here :https://knackforge.com/blog/sujith/ajax-autocomplete-customization-textfield-drupal
The solution suggested @Clive works fine,I tested and it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.