I am building an idea capture website from the Array Shift installation profile of drupal and I'm trying to create a similar entry lookup for when a user starts to create a new idea. Is there a way I can detect when the user is typing text into the title field of the create new idea form (a new content type I created in cck) and then, using ajax, create a dynamic dropdown list of all other posted ideas that are similar based on custom sql queries I create? I've seen this done in many 3rd party COTS tools for idea capture, not to mention right here in Stackoverflow, and I'm hoping to do the same in drupal. Right now, the only ways I can think of to do this is to hack the cck module, which I know is not a good idea, or to somehow hook into that form somehow. I'm not sure how to do that. Do I use hook_form_alter()? I'm using drupal 6.16 and CCK 6.x-2.7. Thanks.
No idea if this is similar to what you want but you can have a CCK field with freetext input that opens a AJAX generated "drop down" field with possible matches. AFAIK this only work with nodes, taxonomie and maybe some other Drupal integrated types. No possibility to use a custom query.
If you need to create something on your own look at the source for this "freetext" fields in the source and adapt it to your needs and/or create a custom CCK field for this special type.
There is a very nice autocomplete system in Drupal, but it's not very well documented if you want to go completely off the beaten path.
I have made good use of the Autocomplete Widgets module. You create a dedicated CCK Text field using this as a widget, then use a PHP snippet to set the allowed values. The allowed values are used to generate the autocomplete options.
I simply dropped a PHP function call in the snippet area. The PHP function I defined in a custom module with queried a remote database and returned the results with drupal_json().
I solved my own problem. Here's what I did:
- set up a drupal.behavior of onchange for the title textfield
- made an ajax call inside that onchange handler
- connected the ajax call to a path that corresponds to a callback function I created in a custom module
- Critical: I had to disable and then re-enable that custom module in order for the callback function to ever be called (the one defined in a hook_menu())
- made a sql query in the callback function that looked for similar ideas in the db
- made the results print out in a markup element I placed just below the title textfield. I wrapped the results in tags that link to their respective idea pages.
And voila, it works like a charm now! I had to piece together about 10 tutorials to figure this out.