You are here

function group_autocomplete_by_type in Group 7

Retrieves autocomplete suggestions for existing groups.

This function will search for all existing groups the user has view access to, optionally filtered by group type.

@todo Individual group access check.

Parameters

GroupType $group_type: (optional) The group type to retrieve suggestions for. The search will traverse all known group types when no type is provided.

string $search: The partial title of the group.

1 call to group_autocomplete_by_type()
group_autocomplete in pages/group.inc
Retrieves autocomplete suggestions for existing groups.
1 string reference to 'group_autocomplete_by_type'
GroupUIController::hook_menu in classes/group.ui_controller.inc
Provides definitions for implementing hook_menu().

File

pages/group.inc, line 34
Page functions for groups.

Code

function group_autocomplete_by_type(GroupType $group_type = NULL, $search = '') {
  $matches = array();
  if ($search) {
    $query = db_select('groups', 'g');
    $query
      ->addField('g', 'gid');
    $query
      ->addField('g', 'title');
    $query
      ->condition('title', db_like($search) . '%', 'LIKE');
    $query
      ->range(0, 10);
    if (!empty($group_type)) {
      $query
        ->condition('type', $group_type->name);
    }
    foreach ($query
      ->execute()
      ->fetchAllKeyed() as $gid => $title) {
      $title = check_plain($title);
      $match = "{$title} (GID: {$gid})";
      $matches[$match] = $match;
    }
  }
  drupal_json_output($matches);
}