0

I have this below code for autocomplete implementation but I just get AJAX error

enter image description here

As you can see there is not anymore details for tracking down the issue. I also checked the logs but seems like this particular error is not logged anywhere which is making me hard to debug. However I suspect (almost confirmed) there is a strong relation between menu path (tdyards/tdyards_searchbox), autocomplete_path (tdyards/tdyards_searchbox/autocomplete_callback)and callback function name (tdyards_searchbox_autocomplete_callback)

Please help me to understand and correct this

Below is the complete code of my module which is sub-module of tdyards

function tdyards_searchbox_menu(){

  $items['tdyards/tdyards_searchbox'] = array(
    'title' => 'Autocomplete (simple)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('autocomplete_form'),
    'access arguments' => array('access user profiles'),
    ); 

  return $items;

}
function autocomplete_form($form, &$form_state) {


  //dpm(cities_autocomplete_callback("A"));
  $form['info'] = array(
    '#markup' => '<div>' . t("This example does a simplest possible autocomplete by username. You'll need a few users on your system for it to make sense.") . '</div>',
  );

  $form['user'] = array(
    '#type' => 'textfield',
    '#title' => t('Choose a user (or a people, depending on your usage preference)'),
    '#autocomplete_path' => 'tdyards/tdyards_searchbox/autocomplete_callback',
  );

  return $form;
}

function tdyards_searchbox_autocomplete_callback($string = "") {

  return drupal_json_output(drupal_map_assoc(array('Data','Theta','Quota','Meta')));

}
1

You've just forgotten to define the path for the autocomplete itself:

function tdyards_searchbox_menu(){

  $items['tdyards/tdyards_searchbox'] = array(
    'title' => 'Autocomplete (simple)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('autocomplete_form'),
    'access arguments' => array('access user profiles'),
    ); 

  $items['tdyards/tdyards_searchbox/autocomplete'] = array(
    'title' => 'Autocomplete',
    'page callback' => 'tdyards_searchbox_autocomplete_callback',
    'access arguments' => array('access user profiles'),
    'type' => MENU_CALLBACK,
  );

  return $items;

}

Add that, clear the caches, and it should work.

0
1

Your #autocomplete_path needs to point to a menu callback, so you'd have something like this

function hook_menu() {
  // autocomplete_path menu item
  $items['tdyards/tdyards_searchbox/autocomplete_callback'] = array(
    'page callback' => 'tdyards_searchbox_autocomplete_callback',
    'type' => MENU_CALLBACK, 
  );
}

..

// automcomplete menu item callback function
function tdyards_searchbox_autocomplete_callback($string = "") {
  return drupal_json_output(drupal_map_assoc(array('Data','Theta','Quota','Meta')));
}

If you are passing in arguments you can add % wildcard to the path and set 'page arguments' => array(1)

2
  • missing 'access arguments', without this the form is not rendered as autocomplete text field – Dudepal Jun 16 '14 at 5:57
  • Hey yes probably, it was just to let you know you needed a menu item for the #auto_complete – davewilly Jun 16 '14 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.