Hello,

I'm looking for a code example for taxonomy autocomplete for Drupal 6. The only one I could find was for 4.7 here http://drupal.org/node/42552

Can anyone please point me to an example or guide to create an autocomplete form for a particular Taxonomy?

TYIA

Comments

nevets’s picture

I you mark a vocabulary as "tags" then it will use auto complete (it is also "auto add").

ludo1960’s picture

...I worded my request wrongly, sorry. I want to build a form for searching Taxonomy terms in a particular Vocabulary, as in the example above.

nevets’s picture

You can use the views module to produce a list, add a filter on "Taxonomy: Term", select auto complete and expose the filter.

ludo1960’s picture

As usual you're spot on!

Just wondering, is there any advantage in creating a module for the form vs the Views approach ie performance wise?

nevets’s picture

While there may be a slight performance improvement with a module over views I generally do not think the difference is enough to warrant writing a module if views does the job.

ludo1960’s picture

Hi Nevets,

From what I jave read on this site, there are 2 pre-defined autocomplete paths built into Drupal 6 core. http://api.drupal.org/api/file/developer/topics/forms_api_reference.html... shows an example of the user autocomlete function but nowhere can I fine a working example of Taxonomy autocomplete (did a search for "Taxonomy autocomplete" and went through 20 pages!)

There must be users out there that have successfully implemented this feature, surely a working example would encourage learners to get to grips with the daunting FAPI and benefit the whole community. (Not a rant, just an observation! :-) )

Anyway, all the best and cheers for your help, I hope someone reads this and can provide code for a working Taxonomy autocomplete custom module.

ludo1960’s picture

Maybe a new Book page is required......We could even call the book page NEVETS NUGGETS What da ya think?
:-)

jlaaker’s picture

After a lot of struggling and looking at the brilliant developers, I solved something I hope will help you:

First you have to use a MENU_CALLBACK:
<%
function yourmodule_menu() {
$items = array();
//a url to let the autocomplete function to run
$items['search/yourmodule-autocomplete'] = array(
'page callback' => 'tax_autocomplete_taxonomy',
'type' => MENU_CALLBACK,
);
return $items;
}
?>
This will trigg the tax_autocomplete_taxonomy() function.
Be aware that you have to visit admin -> build -> module to rebuild the menu. If not, the system will not recognize the url.
<%
function yourmodule_form() {
$form['yourmodule_autocomplete'] = array(
'#type' => 'textfield',
'#title' => t('Find your element'),
'#size' => 15,
'#autocomplete_path' => 'search/yourmodule-autocomplete',
);
$form['submit'] = array(
'#id' => 'yourmodule_autocomplete_submit',
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
?>
This part construct the form and point to the url so the autocomplete will be run.
So then you have to fetch the data from the database:
<%
// Returns an autocomplete list for search keywords.
function tax_autocomplete_taxonomy($string = '') {
$matches = array();
// If the user has started typing something...
if ($string) {
// Pull up relevant results from the database. Here you might want to extend the SQL (doble %% after the searching word to show the suggestions when you start to write)
$result = db_query_range("SELECT fieldname FROM {yourtable} WHERE LOWER(fieldname) LIKE LOWER('%s%%') ORDER BY fieldname DESC", $string, 0, 10);
while ($data = db_fetch_object($result)) {
//$data is the row and you have to convert the data to a HTML-safe string
$matches[$data->fieldname] = check_plain($data->fieldname);
}
}
// Print out the matches in the format expected by autocomplete.js. Send the result to javascript by json.
drupal_json($matches);
}
?>
And then at last you migth use the function:
yourmodule_form_submit() to make something happen after you have clicked the button:

function yourmodule_form_submit($form, &$form_state) {
	$keyword = '';
	// Get the keyword based on the form.
	$keyword = trim($form_state['values']['yourmodule_autocomplete']);
//more code here...
}
summit’s picture

Would be great to have this as patch for a D6 module related to taxonomy, right? Thanks for the code already!
Greetings, Martijn

tpainton’s picture

subscribing

held69’s picture

@ nevets

Sorry to break in here........

but i find it strange that in the option you mentioned (add a filter on "Taxonomy: Term",) i can only choose from one vocabulary.

In the search field i like to create i want all may taxonomy terms in all my vocabularies to be available as result.
I have tried the option 'taxonomy: vocabulary' but this option doesn't have the option of an autocomplete list.

What would be a nice alternative here?

Thanks

aliyayasir’s picture

plz check this module
http://drupal.org/project/batax

Aliya Yasir
Co-founder & CTO
YAS Global Inc.
Email: aliya@yasglobal.com
Web: www.yasglobal.com

darnzen’s picture

The taxonomy autocomplete menu item already exists (in D6).

You use 'taxonomy/autocomplete/' . $vid as your #autocomplete_path and it should work.

$form['tag_example'] = array(
    '#type' => 'textfield',
    '#title' => t('Example autocomplete field'),
    '#autocomplete_path' => 'taxonomy/autocomplete/' . $vid,
    '#maxlength' => 30,
);

EDIT: Forgot the vocabulary id