• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

Drupal: Autocomplete field

Drupal auto complete field lets you select values appear as you type in the text box. Which maximizes the usability and user experience.

Think of a situation, You are creating a custom search function which display author picture, some text containing the word(s) you are typing and node post date.

This can not be achieved by creating a CCK auto complete field since such fields can be added to node types only. You need to create a custom module and write hook_search (no more supported in Drupal 7.x). You have to define a extra attribute in your text box definition: #autocomplete_path. The value this attribute takes is a path defined with hook_menu known to your Drupal site. Any non-existing path assigned to this attribute will result in error. So, first make sure you have defined the path and the callback function with proper permissions.

The auto complete works on 'change' event of the text box. As you type, the path defined in #autocomplete_path calls the function defined in 'callback function' of the menu definition. The function may fetch values from the site database contact third party services.

The function should return an array with key-value pairs. The values matching with the text in the text box will be displayed under the text box and you can select any of the values using either mouse of up-down keys.

Lets have a look at an example for Drupal 6.x

0
Your rating: None
Syndicate content

Recent comments