You are here

function privatemsg_filter_tags_autocomplete in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg_filter/privatemsg_filter.module \privatemsg_filter_tags_autocomplete()
  2. 6 privatemsg_filter/privatemsg_filter.module \privatemsg_filter_tags_autocomplete()
  3. 7 privatemsg_filter/privatemsg_filter.pages.inc \privatemsg_filter_tags_autocomplete()

Return autocomplete results for tags.

Most of this code has been lifted/modified from privatemsg_user_name_autocomplete().

1 string reference to 'privatemsg_filter_tags_autocomplete'
privatemsg_filter_menu in privatemsg_filter/privatemsg_filter.module
Implements hook_menu().

File

privatemsg_filter/privatemsg_filter.pages.inc, line 13
User menu callbacks for privatemsg_filter module.

Code

function privatemsg_filter_tags_autocomplete($string) {

  // 1: Parse $string and build a list of tags.
  $tags = array();
  $fragments = explode(',', $string);
  foreach ($fragments as $tag) {
    $tag = trim($tag);
    $tags[$tag] = $tag;
  }

  // 2: Find the next tag suggestion.
  $fragment = array_pop($tags);
  $matches = array();
  if (!empty($fragment)) {
    $query = _privatemsg_assemble_query(array(
      'tags_autocomplete',
      'privatemsg_filter',
    ), $fragment, $tags);
    $prefix = count($tags) ? implode(", ", $tags) . ", " : '';

    // 3: Build proper suggestions and print.
    foreach ($query
      ->execute() as $tag) {
      $matches[$prefix . $tag->tag . ", "] = $tag->tag;
    }
  }

  // convert to object to prevent drupal bug, see http://drupal.org/node/175361
  drupal_json_output((object) $matches);
}