Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
I made a small modification to Mentions to integrate it with RealName, and to support RealNames with spaces when no end delimiter is given.
I am quite new to drupal so I would not recommend it to produlction use and would be very happy if more experienced ones reviewed my code...
first add a small helper class as wordcountheap.php to the module root for real name sorting:
<?php
class WordCountHeap extends SplMaxHeap {
public function compare($value1, $value2) {
if ( $value1["word_count"] != $value2["word_count"] ) {
return ( $value1["word_count"] - $value2["word_count"] );
}
else {
return ( strlen($value1["name"]->realname) - strlen($value2["name"]->realname) );
}
}
}
?>
Then replace mentions_find_users with this funtion:
<?php
function mentions_find_users($text) {
$users = array();
// If RealName module is installed and we have no delimiter characters
// we apply a different method to extract mentions based on names starting with the word
if ( module_exists("realname") && ( variable_get('mentions_input_suffix',']') == '' ) ) {
$mentions = array();
$mention_start_position = 0;
// We get the words starting with the mention prefix ('@' by default)
while ($mention_start_position !== false) {
$mention_start_position = strpos($text, variable_get('mentions_input_prefix','@'), $mention_start_position);
if ($mention_start_position !== false) {
if ( preg_match( '/' . variable_get('mentions_input_prefix','@') . '(\w+)[ :,\.<&\?!]*/',$text,$match ) ) {
$word = $match[1];
array_push($mentions,$word);
$mention_start_position += strlen($word);
}
}
}
// We set up a heap to sort our names by word count
include_once "wordcountheap.php";
$similar_names = new WordCountHeap;
// We search for user real names which begin with the mentioned word, then we put them into the heap
// with the key "name", together with an auxiliary value "word_count". The heap sorts itself by "word_count" or length when equal.
// This is necessary to get the longest names go first. (To prevent "John Smith" come before and overrde "John Smith jr.")
foreach ($mentions as $mention) {
// We search for real names that begin with the prefixed word then put them into the heap
$query = db_query("SELECT realname, uid FROM {realname} WHERE realname LIKE '%s%'", $mention );
while ( $name = db_fetch_object($query) ) {
$similar_names->insert( array("name" => $name, "word_count" => str_word_count($name->realname) ) );
}
// If there are real names beginning with the word
if (!$similar_names->isEmpty()) {
// And if we find any of them in the text, we add them to the mentioned users list
foreach ($similar_names as $name_object) {
if (preg_match('/'.variable_get('mentions_input_prefix','@').($name_object["name"]->realname).'/',$text,$match) ) {
$user = user_load(array('uid' => $name_object["name"]->uid));
if (is_object($user)) {
$users[] = array(
'text' => $match[0],
'user' => $user
);
break;
}
}
}
}
// If we have no similar real names we try to interpret the word as a username
else {
$user = user_load( array('name' => $mention) );
if (is_object($user)) {
$users[] = array(
'text' => variable_get('mentions_input_prefix','@').$mention,
'user' => $user
);
}
}
}
}
else {
// This is the the original function except for the module_exists("realname") branch of course.
$pattern = variable_get('mentions_input_suffix', ']') != ''
? '/\B('. preg_quote(variable_get('mentions_input_prefix', '[@')) .'|'. preg_quote(check_plain(variable_get('mentions_input_prefix', '[@'))) .')(\#?.*?)('. preg_quote(variable_get('mentions_input_suffix', ']')) .'|'. preg_quote(check_plain(variable_get('mentions_input_suffix', ']'))) .')/'
: '/\B('. preg_quote(variable_get('mentions_input_prefix', '[@')) .'|'. preg_quote(check_plain(variable_get('mentions_input_prefix', '[@'))) .')(\#?\w*)/';
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if ( drupal_substr($match[2], 0, 1) == '#') {
$user = user_load( array('uid' => drupal_substr($match[2], 1)) );
}
else {
// If RealName module is installed we try to interpret name as a realname.
if ( module_exists("realname") ) {
$query = db_query("SELECT uid FROM {realname} WHERE realname = '%s'", $match[2] );
if ($uid = db_fetch_object($query)->uid) {
$user = user_load(array('uid' => $uid));
}
else {
$user = user_load(array('name' => $match[2]));
}
}
else {
$user = user_load(array('name' => $match[2]));
}
}
if (is_object($user)) {
$users[] = array(
'text' => $match[0],
'user' => $user
);
}
}
}
}
return $users;
}
?>
Comments
Comment #1
tomhung commentedIf you have RealName installed you will get access to the [realame] token.
in my case on /admin/settings/filters/[id]/configure
Output:
Prefix = ""
Content = [realname]
Suffix = ""
Comment #2
zps commented10X
But this is only for the output line.
I need to have the same in the input line.
All over the site I use the realname. no one know the user name.
Comment #3
MauPalantir commentedI made a small modification to Mentions to integrate it with RealName, and to support RealNames with spaces when no end delimiter is given.
I am quite new to drupal so I would not recommend it to produlction use and would be very happy if more experienced ones reviewed my code...
first add a small helper class as wordcountheap.php to the module root for real name sorting:
Then replace mentions_find_users with this funtion:
Comment #4
decipheredWorks with D7 with nothing needed to be done, may work in D6 as well as you can change the output value token.