function realname_autocomplete in Real Name 7
Menu callback.
Retrieve a JSON object containing autocomplete suggestions for existing users based on their generated real names.
2 string references to 'realname_autocomplete'
- realname_menu in ./
realname.module - Implements hook_menu().
- realname_menu_alter in ./
realname.module - Implements hook_menu_alter().
File
- ./
realname.module, line 405 - Provides token-based name displays for users.
Code
function realname_autocomplete($field_name, $entity_type, $bundle_name, $string = '') {
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
$matches = array();
// User entityreference autocomplete fields.
if ($instance['widget']['type'] == 'entityreference_autocomplete' && !empty($string)) {
$query = db_select('users', 'u');
$query
->leftJoin('realname', 'rn', 'u.uid = rn.uid');
$query
->fields('u', array(
'uid',
'name',
));
if ($instance['widget']['settings']['match_operator'] == 'CONTAINS') {
$query
->condition(db_or()
->condition('rn.realname', '%' . db_like($string) . '%', 'LIKE')
->condition('u.name', '%' . db_like($string) . '%', 'LIKE'));
}
else {
$query
->condition(db_or()
->condition('rn.realname', db_like($string) . '%', 'LIKE')
->condition('u.name', db_like($string) . '%', 'LIKE'));
}
$query
->range(0, 10);
$uids = $query
->execute()
->fetchCol();
$accounts = user_load_multiple($uids);
foreach ($accounts as $account) {
$matches[t('!account (@uid)', array(
'!account' => format_username($account),
'@uid' => $account->uid,
))] = t('!realname (@username)', array(
'!realname' => format_username($account),
'@username' => $account->name,
));
}
}
elseif (!empty($string)) {
$query = db_select('users', 'u');
$query
->leftJoin('realname', 'rn', 'u.uid = rn.uid');
$query
->fields('u', array(
'uid',
'name',
));
$query
->condition(db_or()
->condition('rn.realname', db_like($string) . '%', 'LIKE')
->condition('u.name', db_like($string) . '%', 'LIKE'));
$query
->range(0, 10);
$uids = $query
->execute()
->fetchCol();
$accounts = user_load_multiple($uids);
foreach ($accounts as $account) {
$matches[$account->name] = t('!realname (@username)', array(
'!realname' => format_username($account),
'@username' => $account->name,
));
}
}
drupal_json_output($matches);
}