function privatemsg_autocomplete in Privatemsg 7
Same name and namespace in other branches
- 5.3 privatemsg.module \privatemsg_autocomplete()
- 5 privatemsg.module \privatemsg_autocomplete()
- 6.2 privatemsg.pages.inc \privatemsg_autocomplete()
- 7.2 privatemsg.pages.inc \privatemsg_autocomplete()
Return autocomplete results for usernames.
Prevents usernames from being used and/or suggested twice.
3 string references to 'privatemsg_autocomplete'
- pm_block_user_menu in pm_block_user/
pm_block_user.module - Implements hook_menu().
- privatemsg_filter_menu in privatemsg_filter/
privatemsg_filter.module - Implements hook_menu().
- privatemsg_menu in ./
privatemsg.module - Implements hook_menu().
File
- ./
privatemsg.pages.inc, line 686 - User menu callbacks for Privatemsg.
Code
function privatemsg_autocomplete($string) {
$names = array();
// 1: Parse $string and build list of valid user names.
$fragments = explode(',', $string);
foreach ($fragments as $name) {
if ($name = trim($name)) {
$names[$name] = $name;
}
}
// 2: Find the next user name suggestion.
$fragment = array_pop($names);
$matches = array();
if (!empty($fragment)) {
$remaining = 10;
$types = privatemsg_recipient_get_types();
foreach ($types as $name => $type) {
if (isset($type['autocomplete']) && is_callable($type['autocomplete']) && privatemsg_recipient_access($name, 'write')) {
$function = $type['autocomplete'];
$return = $function($fragment, $names, $remaining);
if (is_array($return) && !empty($return)) {
$matches = array_merge($matches, $return);
}
$remaining = 10 - count($matches);
if ($remaining <= 0) {
break;
}
}
}
}
// Allow modules to alter the autocomplete list.
drupal_alter('privatemsg_autocomplete', $matches, $names, $fragment);
// Format the suggestions.
$themed_matches = array();
foreach ($matches as $key => $match) {
$themed_matches[$key] = privatemsg_recipient_format($match, array(
'plain' => TRUE,
));
}
// Check if there are any duplicates.
if (count(array_unique($themed_matches)) != count($themed_matches)) {
// Loop over matches, look for duplicates of each one.
foreach ($themed_matches as $themed_match) {
$duplicate_keys = array_keys($themed_matches, $themed_match);
if (count($duplicate_keys) > 1) {
// There are duplicates, make them unique.
foreach ($duplicate_keys as $duplicate_key) {
// Reformat them with unique argument.
$themed_matches[$duplicate_key] = privatemsg_recipient_format($matches[$duplicate_key], array(
'plain' => TRUE,
'unique' => TRUE,
));
}
}
}
}
// Prefix the matches and convert them to the correct form for the
// autocomplete.
$prefix = count($names) ? implode(", ", $names) . ", " : '';
$suggestions = array();
foreach ($themed_matches as $match) {
$suggestions[$prefix . $match . ', '] = $match;
}
// convert to object to prevent drupal bug, see http://drupal.org/node/175361
drupal_json_output((object) $suggestions);
}