You are here

function spaces_user_autocomplete in Spaces 7

Same name and namespace in other branches
  1. 6.3 spaces.admin.inc \spaces_user_autocomplete()
  2. 7.3 spaces.admin.inc \spaces_user_autocomplete()

Override of user/autocomplete. This accomplishes two things:

  • Pushes the result set through db_rewrite_sql() which allows access conditions to be placed on the query.
  • Uses a View, which allows implementing space types to filter the result set when a space is active.
1 string reference to 'spaces_user_autocomplete'
spaces_menu_alter in ./spaces.module
Implements hook_menu_alter().

File

./spaces.admin.inc, line 247

Code

function spaces_user_autocomplete($string = '') {
  $matches = array();
  if ($string && module_exists('views')) {
    views_include('view');
    $view = new view();
    $view->base_table = 'users';
    $handler = $view
      ->new_display('default', 'Defaults', 'default');
    $handler
      ->override_option('filters', array(
      'current' => array(
        'id' => 'current',
        'table' => 'spaces',
        'field' => 'current',
      ),
    ));
    $view
      ->set_display('default');
    $view
      ->build();
    $view->query
      ->add_field('users', 'name', 'name');
    $view->query
      ->add_where(0, "LOWER(users.name) LIKE LOWER('%s%%')", $string);

    // Rebuild queries since we've altered the query object.
    $view->build_info['query'] = $view->query
      ->query();
    $view->build_info['count_query'] = $view->query
      ->query(TRUE);
    $view->build_info['query_args'] = $view->query
      ->get_where_args();
    $view
      ->execute_display();
    foreach ($view->result as $user) {
      $matches[$user->name] = check_plain($user->name);
    }
  }
  drupal_json_output($matches);
}