I have created a custom autocomplete function. The autocomplete field is populated with user name and last name from the content profile.

It is possible that there will be few users with the same name and last name. How I can pass content type ID to Drupal autocomplete, and add this ID to a hidden form field, so I can be more accurate?

I don't want to put the content type ID straight to autocomplete field, so I need some callback to current implementation.

  • Drupal version? – corbacho May 29 '11 at 11:41
  • it is tagged with 6 do that seems to be the Drupal version. – pal4life Sep 14 '11 at 2:52

Thanks to @googletorp advice the solutions is so easy and I want to share it with others. All I need was to overwrite two Drupal autocomplete object functions: found and select.

My autocomplete works on content profile fields. I pass nid of selected profiles to JS:


function messagi_autocomplete($string) {
  global $user;

    $cache = cache_get('messagi_auto_complete_'.$string);
    $items = array();

    if ($cache) {

        $items = $cache->data;

    } else {

        $sql = "
                n.nid AS id
                node as n
                n.type = 'profile'
                u.status = 0
          $result = db_query($sql);
          $disabled = array();
          while($obj = db_fetch_object($result)) {
              $disabled[] = $obj->id;

          $items = array();
          $sql = "
                    profile.nid AS nid,
                    profile.field_profile_name_value AS name,
                    profile.field_profile_surname_value AS surname
                    content_type_profile AS profile
                    lower(profile.field_profile_name_value) LIKE lower('%s%%')
                  OR lower(profile.field_profile_surname_value) LIKE lower('%s%%')
                  LIMIT 10
          $result = db_query($sql, $string, $string);  
          $sql = NULL;
          while($obj = db_fetch_object($result)) {
            if (! in_array($obj->nid, $disabled)) {
                  $key = check_plain($obj->name).' '.check_plain($obj->surname);
                  $items['nids'][$key] = (int) $obj->nid;
                  $items[$key] = $key.', '.check_plain($obj->venue);
            if (! empty($items)) {

                cache_set('messagi_auto_complete_'.$string, $items, 'cache', 1000);     

          $disabled = NULL; 


  print drupal_to_js($items); 


$(document).ready(function() {    if
    (Drupal.jsEnabled) {
            Drupal.jsAC.prototype.found = function (matches) {
              if (!this.input.value.length) {
                return false;

              var ul = document.createElement('ul');
              var ac = this;
              for (key in matches) {
                var li = document.createElement('li');
                //to avoid of showing nids object in the select list 
                if (key !== 'nids') {
                      .html('<div>'+ matches[key] +'</div>')
                      .mousedown(function () {
    ac.select(this); })
                      .mouseover(function () {
    ac.highlight(this); })
                      .mouseout(function () { ac.unhighlight(this); });
                    li.autocompleteValue = key;
                } else {
                 //add all nids to prototype
                    Drupal.jsAC.prototype.nids = matches[key];
                //rest of the code

            Drupal.jsAC.prototype.select = function (node) {
              //get selected value nid and put it on form hidden field
              this.input.value = node.autocompleteValue;
} })

I hope this help somebody in the future. Thanks for the others advice to this code.

  • HI,What file did you add the autocomplete overrides. Also would this work for a webform? If no anything else would you suggest. Here is my question drupal.stackexchange.com/questions/11222/… – pal4life Sep 14 '11 at 2:54
  • I don't understand your questions. I just create a module with .module and js file. Probably it should work wit webform too with no problem. I'll edit my post and provide more code, but must find it first. – Codium Sep 14 '11 at 14:04

If you want to use the default Drupal autocomplete, your best bet it to add the id to the autocomplete and then use additional custom JS to move the id into a hidden field.

The other solution would be to overwrite the prototype functions created by Drupal and add extra handling there.

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.