Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a form with a field which I would like to be an autocomplete field.

I've tried the form_alter hook:

function autocomplete_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == office_node_form) {
    $form['field_office_name'][0]['#autocomplete_path'] = 'autocomplete';
  }
}

The autocomplete path is correct because I use it elsewhere for another field in another form but the field it's not working as autocomplete.

Anything else I should change in the form_alter hook in order to make it work?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Why don't you try Autocomplete Widgets for Text and Number Fields

This module adds 3 autocomplete widgets for CCK fields of type Text and Number.

  • Autocomplete for allowed values list: This widget can be used for Text and Number fields and it takes candidate values from the defined list of Allowed values of the fields. You can even generate your allowed values list using PHP, so the limit to provide a widget with autocomplete features is your imagination!
  • Autocomplete for existing field data: This widget can be used for Text only and it takes candidate values from existing values in the database for that field.
  • (7.x only) Autocomplete for predefined suggestions: This widget can be used for Text only and allows an admin to provide a list of suggestions but still allows users to enter anything they want in a text field. Use this widget to help avoid (but not prevent) variations of the same value. Ex: burger, hamburger, Burger ...
  • (7.x only) Autocomplete for existing field data and some node titles: This widget works just like the "existing field data" widget above except it will also suggest node titles for nodes of a specific content type(s).

share|improve this answer

Replace your code:

$form['field_office_name'][0]['#autocomplete_path'] = 'autocomplete';

With:

$form['field_office_name'][0]['value']['#autocomplete_path'] = 'autocomplete';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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