I cannot get this module getting autocompletion suggestion with taxonomy terms.
- I added a hidden field of taxonomy / all terms for the chosen vocabulary
- I matched the field id and the exposed filter id
- when setting the filter, I correctly get the autocompletion for possibly preselecting terms
- in the form, the autocompletion does not suggest anything, telling either that there need a corresponding field for this filter to work or that there is no results available.

Thanks for help

Comments

jvieille’s picture

Status: Active » Fixed

After trying everything related to this issue without success, I finally found a solution.
I guess that this feature would work with simple, single display views, with few fields and filters. This was definitely not my case. To make it working:
1) remember that the taxonomy terms you are going to filter must be present in every display you will filter (possibly excluded)
2) make sure that all the taxonomy fields you are going to display/filter on a single page have different IDs. Check the field name by hovering on this field - this is the last item of the url - for example tid_1.
For example, in my case, I have 6 taxonomy autocomplete filters on the same page. I had to set up to 6 taxonomy/term fields in order to get the last tid_5 field ID - Views offers no way to freely set the field identifier: it does on his own.
3) use only ONE vocabulary in filters otherwise, Autocomplete won't work
4) Use the first taxonomy filter "Taxonomy: Term Taxonomy term name" together with the last "Taxonomy: Vocabulary Filter the results of "Taxonomy: Term" - any other filter will not work
5) Set the term filter with the "contain" operator, set the filter identifier according to the field name above and check the "autocomplete" checkbox
6) set the vocabulary filter according to the vocabulary you want to filter

Envoy!

jvieille’s picture

Title: Does not work with Taxonomy » How to make Autocomplete working with Taxonomy terms

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jvieille’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

This does not work anymore - seems to relate to Views 2.18 last update. I tried to roll Views back to 2.16, but this is impossible, so I cannot confirm.

1) I have an autocomplete filter for terms of a vocabulary beside one other autocomplete filter for node title

2) I set a field "All terms" for that vocabulary. the id for this field is "tid"

3) I set an exposed filter "Taxonomy term name" with "content" as operator, "tid" as filter identifier

The views_autocomplete_filter.inc code is the following - some tweaks prefixed "JV" included and a dpm call for debug purpose (a single row of the views result for suggestions):

<?php
// $Id: views_autocomplete_filters.module,v 1 vasike Exp $

/**
 * @file
 * Common functions for Autocomplete Filters module.
 */
/**
 * Menu callback; Retrieve a pipe delimited string of autocomplete suggestions.
 */
// JV https://www.drupal.org/node/1303362
//function views_autocomplete_filter($field_name = NULL, $view_name, $view_display, $string = '') {
function views_autocomplete_filter($field_name = NULL, $view_name, $view_display, $arguments, $string = '') {
// 
 static $autocomplete_filter_results;
 $results_id = $field_name.'_'.$view_name.'_'.$view_display.'_'.$string;
 if (isset($autocomplete_filter_results[$results_id])) {
	  drupal_json($autocomplete_filter_results[$results_id]);
		return;
 }
 $matches = array();
 //$view_args = array();
 $view = views_get_view($view_name);
 $view->set_display($view_display);
 // JV https://www.drupal.org/node/1303362
 /* 
  * If there are no arguments present then the query string will change from
  * /field_name/view_name/view_display/arguments/string
  * to
  * /field_name/view_name/view_display/string
  * and therefore the 'argument' parameter will actually be the string we are 
  * looking for. 
  */
  $args = array();
  if ($string == ''){
    $string = $arguments;
  }
  // If there are (comma-separated)arguments, put them back in an 
  // array and set the view arguments
  else{
   	$args = explode(',', $arguments);
   	$view->set_arguments($args);
  }

// end JV
 // force to have no limit for results
 $view->display_handler->set_option('items_per_page', 0);
 $view->handler->items_per_page = $view->pager['items_per_page'] = 0;
 $currentview = $view->display[$view_display];
 // exposed form field name
 if ($currentview->handler->options['defaults']['filters']) {
	 $currentview = $view->display['default'];
 } else {
	 $currentview = $view->display[$view_display];
 }
 $field_filter = $currentview->handler->options['filters'][$field_name];
 // assign string value to the exposed text filter
 $view->exposed_input[$field_filter['expose']['identifier']] = $string;

 $item = $view->get_item($view_display, 'filter', $field_name);
 // overwrite the fields for the view
 $view->display[$view_display]->handler->options['fields'] = array($field_name => $item);
 $view->display[$view_display]->display_options['fields'] = array($field_name => $item);
 $view->execute($view->current_display);
 
 $currentview = $view->display[$view_display];
 // views field handler data
 $field = $currentview->handler->handlers['field'][$field_name];
 $field_alias = $field->field_alias;

 dpm ($view->result[1]);

 foreach ($view->result as $id => $row) {
   // Add a class wrapper for a few required CSS overrides.
	 if (!empty($row->$field_alias)) {
	    $matches[$row->$field_alias] = '<div class="reference-autocomplete">'. $row->$field_alias .'</div>';
	 }
 }
 if (!empty($matches)) {
	 $autocomplete_filter_results[$results_id] = $matches;
 } elseif (!empty($view->result)) {
	 $matches[''] = '<div class="reference-autocomplete">'. t('The %field field of this filter should be present also in the result (could be excluded from display).', array('%field' => $field_filter['expose']['label'])) . '</div>';
 } else {
// JV https://www.drupal.org/node/1303362
//	 $matches[''] = '<div class="reference-autocomplete">'. t('The %string return no results. Please try something else.', array('%string' => $string)) . '</div>';
	 $matches[''] = '<div class="reference-autocomplete">'. t('\'%string\' returned no results. Please try something else.', array('%string' => $string)) . '</div>';
// end JV
 }
 drupal_json($matches);
}

This what I get in dpm

... (Object) stdClass
nid (String, 5 characters ) 14287
og_access_post_og_public (String, 1 characters ) 1
node_language (String, 0 characters )
node_title (String, 134 characters ) Characterization and Estimation of the Variatio...
node_data_field_complement_titre_field_complement_titre_value (NULL)
node_type (String, 4 characters ) file | (Callback) file();
node_vid (String, 5 characters ) 24675
node_node_data_field_manifestation_title (String, 7 characters ) GSI2015
node_node_data_field_manifestation_nid (String, 5 characters ) 11781
node_node_data_field_manifestation_language (String, 0 characters )
node_data_field_edition_revue_field_edition_revue_nid (NULL)
node_data_field_ismemberof_field_ismemberof_nid (NULL)
og_ancestry_nid (String, 5 characters ) 14287
node_created (String, 10 characters ) 1446972480
node_node_data_field_manifestation_node_data_field_date_field_date_value (String, 19 characters ) 2015-10-28T00:00:00
node_node_data_field_manifestation_node_data_field_date_field_date_value2 (String, 19 characters ) 2015-10-30T00:00:00
node_node_data_field_manifestation_type (String, 5 characters ) event
node_node_data_field_manifestation_vid (String, 5 characters ) 24998
node_node_data_field_manifestation_created (String, 10 characters ) 1415634960

- There is no trace of taxonomy information in vews result, the autocomplete will never get filled.
- the effect of ticking the box "Use Autocomplete" is nil: autocomplete is always active as soon as the operator is set to "contains".

By the way, taxonomy autocomplete filter in views should be a very basic requirement. If there is no way to fix this, is there any other solution beside Views hacks' autocomplete filter sub-module?

Thanks for help

colan’s picture

Status: Active » Closed (outdated)