User account menu

  • Log in

Breadcrumb

  • Home
  • DrupalVIP Support
  • Textfield with Autocomplete
Home
DrupalVIP
Freelancer service with great care and enthusiasm.

Main navigation

  • Home
  • Products
  • Blog
  • Support

Textfield with Autocomplete

Autocomplete is implemented in Drupal through AJAX. When users type into a textbox, code on the client page dynamically loads new data from the server (a Drupal website) and uses this data to update the user display (provide a drop-down list of matching options, which the user can select from).

Handily, all the mechanics of exchanging data between client and server and of updating the display are handled by the widget, autocomplete.js. Implementing a particular autocomplete textfield requires two parts:
    1) a caller (the textfield, with special additions) and
    2) a handler, which is a PHP function that parses the request and returns a response.

read more on drupal.org

Two autocomplete functions ship with the Drupal core. Each is referenced by an "autocomplete_path"--the uri to which autocomplete requests are sent.

* user_autocomplete()
Use this function to load matching user names. Autocomplete path: user/autocomplete.
* taxonomy_autocomplete()
Use this function to load matching taxonomy terms from a given vocabulary. Autocomplete path: taxonomy/autocomplete.

If one of these matches your needs, then all you need to do is include the special #autocomplete_path selector in a form field. Here's an example for user autocomplete (from comment.module):

        $form['admin']['author'] = array(
          '#type' => 'textfield',
          '#title' => t('Authored by'),
          '#size' => 30,
          '#maxlength' => 60,
          '#autocomplete_path' => 'user/autocomplete',
          '#default_value' => $author,
          '#weight' => -1,
        );

 

For taxonomy autocomplete, include a vocabulary id, as in this example from taxonomy.module:

        $form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield',
          '#title' => $vocabulary->name,
          '#description' => $help,
          '#required' => $vocabulary->required,
          '#default_value' => $typed_string,
          '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
          '#weight' => $vocabulary->weight,
          '#maxlength' => 1024,
        );

 

For Drupal 7 the taxonomy_autocomplete function takes as parameter the field_name, which is the name of the term reference field. This is the field name of term reference assigned to entity(i.e. node) to be edited. A quick verification may help by manually making a request via http://site.com/taxonomy/autocomplete/@field_name from a browser. If it returns '[]' then it is working, otherwise. It will provide an error message that can help troubleshoot further.

Example:

  $form['example'] = array(
    '#type' => 'textfield',
    '#title' => t('This is the Title'),
    '#autocomplete_path' => 'taxonomy/autocomplete/<strong>taxonomy_vocabulary_1</strong>',
    '#maxlength' => 30,
  );

where taxonomy_vocabulary_1 is the machine name of the taxonomy reference field.

 

Read more...

 
Code Snippet
Read More
Tags
Ajax
Drupal
Code Example
Drupal 7

The Freelancer Assistance

Fullstack Service

Fullstack Service

Drupal Training

drupal training session

Proactive Maintenance

Proactive Maintenance

Drupal Development

Drupal Development

Site Management

Site Management

Drupal Shared Space

Drupal Shared Space
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP - All rights reserved

Developed & Designed by Yuval Ben-Hur