Community & Support

Taxonomy autocomplete shows terms and its parents

Hi again,

I have a classical taxonomy with locations (around 3500 terms like USA, Spain, Barcelona, Rome, Hyde Park, Brussels, ...).

In my video sites, I have an autocomplete field where the user will start typing the location or place and then he can select one of the terms that will be displayed. Until here, no problem.

The point is that there are terms with the same name, but in different countries/places, so I need to know which one I'm selecting.

For example, there's a city called Barcelona in Spain, but there's also a city called Barcelona in Mexico.
That said, it's not nice to see two "Barcelona" terms when typing "Bar" or "Barcel", because you can't be sure if your video will be related to the Spanish city or the Mexican.

So my query goes here: It is possible (how can I...) to display all the parents, in order, of the matching terms??

In that example, I would like to see, when typing "bar" something like
"Barcelona, Spain"
"Barcelona, Mexico"
and then the user will just select one, like always.

I really need the autocomplete field, as browsing the taxonomy with modules like HS is nice but not useful since the user may not know if Barcelona is below Catalonia or just inside Spain (so the users, usually, will not be able to find the terms).

Thank you!

Comments

I just created a function on

I just created a function on my template.php to get the parents of the selected term, but this is just displayinh on my node theme page.
Any option to also use this function or another similar while using the autocomplete field??

hook_form_alter

With a custom module, using a form_alter hook, you can intercept form elements. This would allow you to set the autocomplete path value.

Then copy the taxomony menu item for the original autocomplete path value, and the page callback function that this points to, into your module.

Update the autocomplete path to something different, and change the function names to that of your module. Change the function as per your theme and you should be good to go.

<?php
// file taxonomy_fix.module
/**
* Implementation of hook_menu().
*/
function taxonomy_fix_menu($may_cache) {
 
$items = array();

  if (
$may_cache) {
   
$items[] = array('path' => 'taxonomy_fix/autocomplete',
     
'title' => t('Autocomplete taxonomy'),
     
'callback' => 'taxonomy_fix_autocomplete',
     
'access' => user_access('access content'),
     
'type' => MENU_CALLBACK);
  }
  return
$items;
}
/**
* Helper function for autocompletion
*/
function taxonomy_fix_autocomplete($vid, $string = '') {
// your code here
}
?>

Alan Davison
www.caignwebs.com.au

Alan Davison
Back roads somewhere in South America

A few more details...

Thanks! I know how to write a hook form alter and I think I could implement the code myself.
I'm a bit lost with the other part, the one where you said to "copy the taxonomy menu item from the original..."
I'm using the autocomplete field in the edit page of one node, so not sure of what role the menu is playing in here...
I can't get the last part either -> "change the function as per your theme"... uhm... any extra help that could point me better in the correct direction?
Thanks!

Module

http://drupal.org/project/taxiselect

Thank you Alan!
This is working just great!