I'm trying to insert an autocomplete form on a node with no success. Help is highly appreciated. Thank you in advance. The node where the following code should be inserted is 'content/drupal-test-form-autocomplete-simple'.
<?php
function autocomplete_user($form, &$form_state) {
$form = array();
$form['item'] = array(
'#type'=> 'textfield',
'#title'=> t('Select the entry'),
'#autocomplete_path'=> 'content/drupal-test-form-autocomplete-simple',
);
return $form;
}
//Output Form:
$output = drupal_get_form('autocomplete_user');
print drupal_render($output);
$items['content/drupal-test-form-autocomplete-simple'] = array(
'page callback'=> 'nodes_autocomplete',
'access callback'=> 'user_access',
'access arguments'=> array('administer users'),
'type'=> MENU_CALLBACK,
);
function nodes_autocomplete($string = "") {
$items = array();
if ($string) {
$result = db_select('users')
->fields('users', array('name', 'uid'))
->condition('name', db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
foreach ($result as $user) {
// In the simplest case (see user_autocomplete), the key and the value are the same. Here we'll display the
// uid along with the username in the dropdown.
$items[$user->name] = check_plain($user->name) . " (uid=$user->uid)";
}
}
drupal_json_output($items);
}
?>