Hi, I am writing a Drupal module for simple browser-based video editing. A user types a phrase into an textfield form element, which performs an autocomplete search to a table in the database for similar phrases. This part is easy, thanks to Drupal. But the hard part is that I need a second piece of information simultaneously extracted from the matched row in the database: The url to the media file.

My db structure:
The module creates table "colingo_media" with fields "title", "url", etc.

The issue wireframed:
1) I type "my name is p" into an autocomplete textfield.
2) The db is queried to look for all rows with title "my name is p" and returns "my name is phil" as the only possible match.
3) The user clicks on "my name is phil".

At this point, the URL for the matched row with title "my name is phil" needs to be returned to jQuery, where I'll add it as an attribute to the form element.

my precise question

How can I return multiple objects within the Drupal autocomplete framework, one as the item to be autocompleted, and the other as a hidden jQuery object?

the code i'm working with:

function colingo_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT title FROM {colingo_media} WHERE LOWER(title) LIKE LOWER('%s%%')", $string, 0, 10);
    while ($phrase = db_fetch_object($result)) {
      $matches[$phrase->title] = check_plain($phrase->title);
    }
  }

  print drupal_to_js($matches);
}

Many thanks!!

Comments

criznach’s picture

I wrote a new autocomplete behavior for Drupal 5 based on the built-in one that returned the data I needed. In my case, I matched records by title and stuffed their unique ID into a hidden field when selected. I couldn't use just the titles because they were not guaranteed to be unique.

This would be a killer feature for D7 that I'd like to work on. Unfortunately it's too late for D6.

Chris.

andybrace’s picture

Hi Chris

I am in need of something very similar to the solution you posted, however as I haven't done much work with jquery before I am unsure where to start in order to add the code to populate the hidden field. Could you post the code you use, it would be really handy!

thanks

lionstone’s picture

Thanks to Chris, I have a live demo of this now working at http://www.colingo.org/