You are here

function rules_autocomplete_tags in Rules 7.2

AJAX page callback to load tag suggestions.

Largely copied from taxonomy_autocomplete().

1 string reference to 'rules_autocomplete_tags'
rules_menu in ./rules.module
Implements hook_menu().

File

ui/ui.forms.inc, line 970
Rules User Interface forms.

Code

function rules_autocomplete_tags($tags_typed = '') {

  // The user enters a comma-separated list of tags. We only autocomplete the
  // last tag.
  $tags_typed = drupal_explode_tags($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $tag_matches = array();
  if ($tag_last != '') {
    $query = db_select('rules_tags', 'rt');

    // Do not select already entered terms.
    if (!empty($tags_typed)) {
      $query
        ->condition('rt.tag', $tags_typed, 'NOT IN');
    }

    // Select rows that match by tag name.
    $tags_return = $query
      ->distinct()
      ->fields('rt', array(
      'tag',
    ))
      ->condition('rt.tag', '%' . db_like($tag_last) . '%', 'LIKE')
      ->groupBy('rt.tag')
      ->range(0, 10)
      ->execute()
      ->fetchCol('rt.tag');
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
    foreach ($tags_return as $name) {
      $n = $name;

      // Tag names containing commas or quotes must be wrapped in quotes.
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
        $n = '"' . str_replace('"', '""', $name) . '"';
      }
      $tag_matches[$prefix . $n] = check_plain($name);
    }
  }
  drupal_json_output($tag_matches);
}