You are here

function taxonomy_autocomplete in Drupal 4

Same name and namespace in other branches
  1. 5 modules/taxonomy/taxonomy.module \taxonomy_autocomplete()
  2. 6 modules/taxonomy/taxonomy.pages.inc \taxonomy_autocomplete()
  3. 7 modules/taxonomy/taxonomy.pages.inc \taxonomy_autocomplete()

Helper function for autocompletion

1 string reference to 'taxonomy_autocomplete'
taxonomy_menu in modules/taxonomy.module
Implementation of hook_menu().

File

modules/taxonomy.module, line 1341
Enables the organization of content into categories.

Code

function taxonomy_autocomplete($vid, $string = '') {

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  // This regexp allows the following types of user input:
  // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
  $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  preg_match_all($regexp, $string, $matches);
  $array = $matches[1];

  // Fetch last tag
  $last_string = trim(array_pop($array));
  if ($last_string != '') {
    $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10);
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
    $matches = array();
    while ($tag = db_fetch_object($result)) {
      $n = $tag->name;

      // Commas and quotes in terms are special cases, so encode 'em.
      if (preg_match('/,/', $tag->name) || preg_match('/"/', $tag->name)) {
        $n = '"' . preg_replace('/"/', '""', $tag->name) . '"';
      }
      $matches[$prefix . $n] = check_plain($tag->name);
    }
    print drupal_to_js($matches);
    exit;
  }
}