diff options
-rw-r--r-- | misc/autocomplete.js | 6 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.admin.inc | 2 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.pages.inc | 40 |
3 files changed, 35 insertions, 13 deletions
diff --git a/misc/autocomplete.js b/misc/autocomplete.js index aed6628b3..706a0cf2f 100644 --- a/misc/autocomplete.js +++ b/misc/autocomplete.js @@ -255,6 +255,12 @@ Drupal.ACDB.prototype.search = function (searchString) { var db = this; this.searchString = searchString; + // See if this string needs to be searched for anyway. + searchString = searchString.replace(/^\s+|\s+$/, ''); + if (searchString.charAt(searchString.length - 1) == ',') { + return; + } + // See if this key has been searched for before. if (this.cache[searchString]) { return this.owner.found(this.cache[searchString]); diff --git a/modules/taxonomy/taxonomy.admin.inc b/modules/taxonomy/taxonomy.admin.inc index 014b7152c..c37e31feb 100644 --- a/modules/taxonomy/taxonomy.admin.inc +++ b/modules/taxonomy/taxonomy.admin.inc @@ -751,7 +751,7 @@ function taxonomy_form_term(&$form_state, $vocabulary, $edit = array()) { '#type' => 'textarea', '#title' => t('Synonyms'), '#default_value' => implode("\n", taxonomy_get_synonyms($edit['tid'])), - '#description' => t('Synonyms of this term, one synonym per line.')); + '#description' => t('One synonym per line. Text input term selection widgets will show terms whose synonyms match the entered value as suggestions.')); $form['advanced']['weight'] = array( '#type' => 'textfield', '#title' => t('Weight'), diff --git a/modules/taxonomy/taxonomy.pages.inc b/modules/taxonomy/taxonomy.pages.inc index bbf31178c..e2cdf516e 100644 --- a/modules/taxonomy/taxonomy.pages.inc +++ b/modules/taxonomy/taxonomy.pages.inc @@ -122,36 +122,52 @@ function taxonomy_term_edit($term) { /** * Helper function for autocompletion */ -function taxonomy_autocomplete($vid, $string = '') { +function taxonomy_autocomplete($vid = 0, $tags_typed = '') { // The user enters a comma-separated list of tags. We only autocomplete the last tag. - $array = drupal_explode_tags($string); + $tags_typed = drupal_explode_tags($tags_typed); + $tag_last = drupal_strtolower(array_pop($tags_typed)); - // Fetch last tag - $last_string = trim(array_pop($array)); $matches = array(); - if ($last_string != '') { + if ($tag_last != '') { $query = db_select('taxonomy_term_data', 't'); $query->addTag('term_access'); - - $tags = $query + $query->leftJoin('taxonomy_term_synonym', 'ts', 't.tid = ts.tid'); + // Don't select already entered terms. + if (count($tags_typed)) { + $query->condition('t.name', $tags_typed, 'NOT IN'); + } + $tags_return = $query ->fields('t', array('tid', 'name')) ->condition('t.vid', $vid) - ->where("LOWER(t.name) LIKE LOWER(:last_string)", array(':last_string' => '%' . $last_string . '%')) + // Select rows that either match by term or synonym name. + ->condition(db_or() + ->where("LOWER(t.name) LIKE :last_string", array(':last_string' => '%' . $tag_last . '%')) + ->where("LOWER(ts.name) LIKE :last_string", array(':last_string' => '%' . $tag_last . '%')) + ) ->range(0, 10) ->execute() ->fetchAllKeyed(); - $prefix = count($array) ? implode(', ', $array) . ', ' : ''; + $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : ''; - foreach ($tags as $tid => $name) { + // We use two arrays to make sure synonym suggestions appear last. + $term_matches = $synonym_matches = array(); + foreach ($tags_return as $tid => $name) { $n = $name; // Commas and quotes in terms are special cases, so encode 'em. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) { $n = '"' . str_replace('"', '""', $name) . '"'; } - $matches[$prefix . $n] = check_plain($name); + // Inform the user his query matched a synonym rather than a term. + if (strpos(drupal_strtolower($name), $tag_last) === FALSE) { + $name = t('Did you mean %suggestion', array('%suggestion' => $name)); + $synonym_matches[$prefix . $n] = filter_xss($name); + } + else { + $term_matches[$prefix . $n] = filter_xss($name); + } } } - drupal_json($matches); + drupal_json(array_merge($term_matches, $synonym_matches)); } |