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
}

copyright 2012 10jumps Llc.

copyright 2012 10jumps LLC.