views:

36

answers:

3

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.

A: 

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.

DrColossos
I don't really need autocomplete because I don't want the similar ideas to appear in the textbox. Instead, I want it to appear under the textbox, just echoed out on the screen in its own div container where the user can click on links of similar ideas that were already posted in order to verify that the idea they were about to post isn't already answered, just like on this website. I need some way to create an event handler that fires whenever the user types something into the title or body fields.
Dylan West
I'd like to take the text they type into the field and use that as part of a LIKE statement for a sql query to allow me to search against several different places: tags, titles and bodies of other nodes, etc.
Dylan West
+1  A: 

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().

Grayside
I tried this autocomplete widgets and it didn't work. I got no error messages, it was just a silent failure. I tried altering the existing create idea form with code and i tried creating a new textfield with cck and the autocomplete just wouldn't work on either one. I'm not sure why.
Dylan West
You want to use "Autocomplete for allowed values list". If you need to troubleshoot it, I suggest going to the project issues queue.
Grayside
Using that option works. When I tried it the first time, I tried the existing values option. Thanks. But now, how can I get these autocomplete works linkable? That's what i really need to do.
Dylan West
A: 

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.

Dylan West
That's usually a sign you have good material on which to write a tutorial ;)
Grayside