Create the custom autocomplete field

17 Jan

Create the custom autocomplete field

in Drupal Planet

Create the custom autocomplete field using the custom module.
How to create a custom autocomplete form and to get the user names or or anything else?
Here is the example.
/**
*Implementation of hook_menu().
*/

$items['custom_automplete_form'] = array(
'title' => 'Custom autocomplete form',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_autocomplete_form'),
'access arguments' => array('access autocomplete'),
'type' => MENU_NORMAL_ITEM
);
/**
*Implementation of function custom_autocomplete_form()
*/

function custom_autocomplete_form(){
$form = array();
$form['customauto'] = array(
'#type' => 'textfield',
'#autocomplete_path' => 'custom/autocomplete',
'#description' => t('Please type any letter.'),
);
return $form;
}
#autocomplete_path – Custom path to get the autocomplete result.
/**
*Implementation of hook_menu()
**/

$items['custom/autocomplete'] = array(
'title' => 'Custom autocomplete',
'page callback' => 'custom_autocomplete',
'access arguments' => array('access autocomplete'),
'type' => MENU_CALLBACK
);

/**
*Implemtation of function custom_autocomplete($string = '')
**/

function custom_autocomplete($string = '') {
$matches = array();
if ($string) {
$result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10); // only 10 results will show
while ($user = db_fetch_object($result)) {
$matches[$user->name] = check_plain($user->name);
}
}
drupal_json($matches); //Returns the data in JSON format
}

Comments

Thank for your share, by adzzikr (not verified)

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

copyright 2011 10jumps Llc.

copyright 2011 10jumps LLC.