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);
}



?>

Comments

Version: 7.22 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.