You are here

function content_taxonomy_autocomplete_load in Content Taxonomy 6

Same name and namespace in other branches
  1. 5 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_load()
  2. 6.2 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_load()

Retrieve a pipe delimited string of autocomplete suggestions

Parameters

String Fieldname:

Integer TID of a parent (optional):

BOOLEAN whether a multiple field or not:

STRING typed input:

1 string reference to 'content_taxonomy_autocomplete_load'
content_taxonomy_autocomplete_menu in ./content_taxonomy_autocomplete.module
Implementation of hook_menu

File

./content_taxonomy_autocomplete.module, line 269
Defines a widget type for content_taxonomy with autocomplete

Code

function content_taxonomy_autocomplete_load($field_name, $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
  $content_type_info = _content_type_info();
  $vid = $content_type_info['fields'][$field_name]['vid'];
  $tid = content_taxonomy_field_get_parent($content_type_info['fields'][$field_name]);

  // If the menu system has splitted the search text because of slashes, glue it back.
  if (func_num_args() > 2) {
    $args = func_get_args();
    $string .= '/' . implode('/', array_slice($args, 2));
  }

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

  // Fetch last tag
  $last_string = trim(array_pop($array));
  $matches = array();
  if ($last_string != '') {
    if ($tid) {
      $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t \n        LEFT JOIN {term_synonym} s ON t.tid = s.tid\n        INNER JOIN {term_hierarchy} h ON  t.tid = h.tid\n        WHERE h.parent = %d \n        AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), $tid, $last_string, $last_string, 0, 10);
    }
    else {
      $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t \n        LEFT JOIN {term_synonym} s ON t.tid = s.tid\n        WHERE t.vid = %d \n        AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), $vid, $last_string, $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);
}