I've come across this same problem on two different machines with two different sites, but I haven't seen it reported anywhere else, so maybe I'm just doing something wrong.

In a form, I want to use a textfield with an autocomplete path that points to a taxonomy vocabulary. I use this syntax:

  $form['location'] = array(
    '#title' => t('Location'),
    '#type' => 'textfield',
    '#autocomplete_path' => 'taxonomy/autocomplete/4',
    '#description' => t('Enter the location. Separate terms with commas. (eg. Los Angeles, California)')
  );

When I type anything in the text field, I get "An AJAX HTTP error occurred. HTTP Result Code: 500".

From the log message:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND (t.name LIKE '%at%' ESCAPE '\\') LIMIT 10 OFFSET 0' at line 1: SELECT t.tid AS tid, t.name AS name FROM {taxonomy_term_data} t WHERE (t.vid IN ()) AND (t.name LIKE :db_condition_placeholder_0 ESCAPE '\\') LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => %at% ) in taxonomy_autocomplete() (line 141 of C:\xampp\htdocs\livemusic\modules\taxonomy\taxonomy.pages.inc)

The problem seems to be with the $field_name parameter in the taxonomy_autocomplete function in taxonomy.pages.inc. In the SQL statement in the error message, no vid is set.

I managed to get the functionality I wanted by building my own autocomplete function.

I added the following to hook_menu

  $items['example/autocomplete/%'] = array(
    'title' => 'Example taxonomy autocomplete',
    'page callback' => '_example_autocomplete',
    'page arguments' => array(2),
    'access arguments' => array('use autocomplete'),
    'type' => MENU_CALLBACK
  );

And then I created a function called _example_autocomplete and copied the taxonomy_autocomplete function from taxonomy.pages.inc. I then made a couple small changes. For my case, I just need to specify one vid, so I renamed the $field_name parameter to $vid. I got rid of the "$field = field_info_field($field_name);" line, plus all of the code used to build the $vids array. Then I changed line 138 from:
->condition('t.vid', $vids)
to
->condition('t.vid', $vid, '=')
where $vid is passed as a url parameter.

I can then call this autocomplete function for a textfield type using "'#autocomplete_path' => 'example/autocomplete/4',"

Comments

xjm’s picture

Version: 7.10 » 8.x-dev
Issue tags: +Needs backport to D7, +Needs steps to reproduce
Unitoch’s picture

We (zgear and I) were able to reproduce this error by doing the following steps:

1. Download a clean copy of Drupal 8.
2. Create a test taxonomy.
3. Create a module that includes the form code above and displays the form on a page.
4. Alter the code so that the autocomplete path contains the correct taxonomy id.
5. Enter in part of a term in the vocabulary, AJAX error results.

Note: We did not see the relevant error in the logs, but the AJAX error matches up.

Unitoch’s picture

Removing "Needs steps to reproduce" tag

xjm’s picture

Issue tags: +Needs tests

Awesome, thanks @Unitoch and @Zgear!

Next let's get an automated test based on the steps in #2.

adharris’s picture

Category: bug » support
Status: Active » Postponed (maintainer needs more info)

It looks like you might be using the autocomplete path for the wrong type of thing.

The taxonomy autocomplete path is for the taxonomy term field type, and therefore expects a field name as the first parameter, not vocabulary id. so instead of taxonomy/autocomplete/4 usetaxonomy/autocomplete/field_name where field name corresponds to an existing taxonomy term field. If you don't have a suitable taxonomy field on a content type somewhere, the custom callback in your module will work fine.

If I'm missing something, let me know.

adharris’s picture

Issue summary: View changes

changed "->condition('t.vid', $vid)" to ->condition('t.vid', $vid, '=')

tkoleary’s picture

Please follow progress on: #2346973: Improve usability, accessibility, and scalability of long select lists

Changing to Select2 may render this issue obsolete

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

cilefen’s picture

Is this a bug or is #5 the answer?

amateescu’s picture

Version: 8.1.x-dev » 7.x-dev
Status: Postponed (maintainer needs more info) » Closed (works as designed)
Issue tags: -Needs tests, -Needs backport to D7

I think #5 is the answer. Also, the 'taxonomy/autocomplete' menu item does not exist in Drupal 8 anymore since #1847596: Remove Taxonomy term reference field in favor of Entity reference.