-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_filter.module
61 lines (57 loc) · 1.39 KB
/
user_filter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* @file
*/
/**
* Implements hook_filter_info().
*/
function user_filter_filter_info() {
$filters = array();
$filters['user_filter'] = array(
'title' => t('User filter'),
'description' => t('View user info by "@username"'),
'process callback' => 'user_filter_filter_process',
'cache' => FALSE,
);
return $filters;
}
/**
* Filter process.
*/
function user_filter_filter_process($text, $filter, $format) {
if (!empty($text)) {
$users = user_filter_mention($text);
if (!empty($users)) {
foreach ($users as $user) {
$user_profile = l('@' . $user->name, drupal_get_path_alias('user/' . $user->uid), array('attributes' => array('class' => array('user_filter'), 'data-user' => array($user->name))));
$text = preg_replace('/@' . $user->name . '/', $user_profile, $text);
}
}
return $text;
}
else {
return $text;
}
}
/**
* User Filter API.
*
* Check Users in text by "@username".
*
* Function "user_filter_mention($text)".
* Return an array with User objects.
*/
function user_filter_mention($text) {
if (!empty($text)) {
$users = array();
preg_match_all('/[@][\w\d]+/', $text, $matches);
foreach ($matches[0] as $match) {
$name = drupal_substr($match, 1, $length = NULL);
$user = user_load_by_name($name);
if (is_object($user)) {
$users[] = $user;
}
}
return $users;
}
}