I'm trying to change the autocomplete path of a node reference field (References module) with a custom module using hook_form_alter().
I have no problem changing any property of the field except the ["#autocomplete_path']. when I set another value for this, the field loses its autocomplete feature. (The hidden autocomplete field isn't there anymore as well) I find this totally weird, and haven't found anything about this in issue queues or anything like that.

Just FYI, I've setup a new MENU_CALLBACK path with hook_menu() and a callback function, so I'll be able to return JSON suggestions depending on some custom parameters and the value of another field.

3 Answers 3


On a stock installation of Drupal 7 and references 7.x-2.0-beta3 I was able to use the following form alter to successfully change the autocomplete path for a node reference field on the article content type:

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    $form['field_node_ref'][LANGUAGE_NONE][0]['nid']['#autocomplete_path'] = 'another/url';

Another option is to ignore the form and use hook_menu_alter to change the callback for node_reference/autocomplete/%/%/%. Your callback should accept $entity_type, $bundle, $field_name which you can use to filter out which requests you want to respond to.

  • well, that's what i'm already using... that's the weird thing about it. the field i'm trying to change is in a field collection, so i added a test field to make sure it's not a field_collection bug or anything. with no results -------------- function MY_MODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) { $form['field_profile_test'][LANGUAGE_NONE][0]['nid']['#autocomplete_path'] = 'my_module/zip/autocomplete'; }
    – 30equals
    Commented Oct 24, 2011 at 8:23
  • Is this a field on a profile2 entity? Maybe something is weird about changing an entity vs a node. Are you sure that no AJAX request is being made at all? Since I proved it works on nodes on stock drupal+references my guess is something else is interfering. Commented Oct 24, 2011 at 10:53
  • No it's not a profile2 entity field. although i have profile2 installed. I just tried it on a clean D7 install with the article node type as well and it's working so it must be some weird module interference i guess...? thanx for checking it out btw!
    – 30equals
    Commented Oct 24, 2011 at 22:33
  • how can i form alert for this "'#autocomplete_path' => 'user/autocomplete'" Commented Feb 14, 2015 at 11:48

You should ensure your custom callback function is fetching correct json response. we can do so by calling the page callback path from the browser itself.

If its correct then check, The last param is always string in the page callback function which you need not to mention in form_alter or in hook_menu path but drupal will add the last param automatically while calling the function. I guess you missed the last param in your case.

Menu callback function should be like :

  $items['autocomplete/node/%'] = array(
      'title' => 'Custom autocomplete node reference',
      'page callback' => 'pms_custom_node_autocomplete',
      'page arguments' => array(**2**),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,

Page callback function should be like :

function pms_custom_node_autocomplete($node_type, $string = "") {
  // Get $string here to use in LIKE statement or whatever.

In my own case (D7.20), I had to set my hook_form_alter() like that:

function module_form_alter(&$form, &$form_state, $form_id){
    if ($form_id == "event_node_form"){
        $form['field_event_location']['und'][0]['value']['#autocomplete_path'] = 'module/autocomplete';
        return $form;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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