You are here

function i18ntaxonomy_autocomplete in Internationalization 6

Helper function for autocompletion.

@ TODO Optimized localization. We cannot just i18nstrings() huge lists of terms.

1 string reference to 'i18ntaxonomy_autocomplete'
i18ntaxonomy_menu_alter in i18ntaxonomy/i18ntaxonomy.module
Implementation of hook_menu_alter().

File

i18ntaxonomy/i18ntaxonomy.pages.inc, line 124
Page callbacks for the taxonomy module, i18n remake.

Code

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

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  $array = drupal_explode_tags($string);

  // Is this vocabulary localizable?
  $localizable = i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE;

  // Fetch last tag.
  $last_string = trim(array_pop($array));
  $matches = 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) . ', ' : '';
    while ($tag = db_fetch_object($result)) {
      $n = $tag->name;

      // Commas and quotes in terms are special cases, so encode 'em.
      if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
        $n = '"' . str_replace('"', '""', $tag->name) . '"';
      }
      $matches[$prefix . $n] = check_plain($tag->name);
    }
  }
  drupal_json($matches);
}