By ludo1960 on
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
I you mark a vocabulary as
I you mark a vocabulary as "tags" then it will use auto complete (it is also "auto add").
Perhaps...
...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.
You can use the views
You can use the views module to produce a list, add a filter on "Taxonomy: Term", select auto complete and expose the filter.
Thanks Nevets
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?
While there may be a slight
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.
JFYI
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.
Eureka!!!
Maybe a new Book page is required......We could even call the book page NEVETS NUGGETS What da ya think?
:-)
One example
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:
Would be great to have this
Would be great to have this as patch for a D6 module related to taxonomy, right? Thanks for the code already!
Greetings, Martijn
subscribing
subscribing
@ nevets Sorry to break in
@ 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
working with large taxonomy terms
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
taxonomy/autocomplete already defined in taxonomy module
The taxonomy autocomplete menu item already exists (in D6).
You use 'taxonomy/autocomplete/' . $vid as your #autocomplete_path and it should work.
EDIT: Forgot the vocabulary id