1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_field_user_roles.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Field handler to provide a list of roles.
|
10
|
*
|
11
|
* @ingroup views_field_handlers
|
12
|
*/
|
13
|
class views_handler_field_user_roles extends views_handler_field_prerender_list {
|
14
|
|
15
|
/**
|
16
|
* {@inheritdoc}
|
17
|
*/
|
18
|
public function construct() {
|
19
|
parent::construct();
|
20
|
$this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
|
21
|
}
|
22
|
|
23
|
function option_definition() {
|
24
|
$options = parent::option_definition();
|
25
|
$options['display_roles'] = array('default' => array());
|
26
|
|
27
|
return $options;
|
28
|
}
|
29
|
|
30
|
function options_form(&$form, &$form_state) {
|
31
|
$roles = user_roles(TRUE);
|
32
|
unset($roles[DRUPAL_AUTHENTICATED_RID]);
|
33
|
if (!empty($roles)) {
|
34
|
$form['display_roles'] = array(
|
35
|
'#type' => 'checkboxes',
|
36
|
'#title' => t('Only display the selected roles'),
|
37
|
'#description' => t("If no roles are selected, all of the user's roles will be shown."),
|
38
|
'#options' => $roles,
|
39
|
'#default_value' => $this->options['display_roles'],
|
40
|
);
|
41
|
}
|
42
|
parent::options_form($form, $form_state);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* {@inheritdoc}
|
47
|
*/
|
48
|
public function query() {
|
49
|
$this->add_additional_fields();
|
50
|
$this->field_alias = $this->aliases['uid'];
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* {@inheritdoc}
|
55
|
*/
|
56
|
public function pre_render(&$values) {
|
57
|
$uids = array();
|
58
|
$this->items = array();
|
59
|
|
60
|
foreach ($values as $result) {
|
61
|
$uids[] = $this->get_value($result, NULL, TRUE);
|
62
|
}
|
63
|
|
64
|
if ($uids) {
|
65
|
$rids = array_filter($this->options['display_roles']);
|
66
|
if (!empty($rids)) {
|
67
|
$result = db_query("SELECT u.uid, u.rid, r.name FROM {role} r INNER JOIN {users_roles} u ON u.rid = r.rid WHERE u.uid IN (:uids) AND r.rid IN (:rids) ORDER BY r.name",
|
68
|
array(':uids' => $uids, ':rids' => $rids));
|
69
|
}
|
70
|
else {
|
71
|
$result = db_query("SELECT u.uid, u.rid, r.name FROM {role} r INNER JOIN {users_roles} u ON u.rid = r.rid WHERE u.uid IN (:uids) ORDER BY r.name",
|
72
|
array(':uids' => $uids));
|
73
|
}
|
74
|
foreach ($result as $role) {
|
75
|
$this->items[$role->uid][$role->rid]['role'] = check_plain($role->name);
|
76
|
$this->items[$role->uid][$role->rid]['rid'] = $role->rid;
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* {@inheritdoc}
|
83
|
*/
|
84
|
public function render_item($count, $item) {
|
85
|
return t($item['role']);
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* {@inheritdoc}
|
90
|
*/
|
91
|
public function document_self_tokens(&$tokens) {
|
92
|
$tokens['[' . $this->options['id'] . '-role' . ']'] = t('The name of the role.');
|
93
|
$tokens['[' . $this->options['id'] . '-rid' . ']'] = t('The role ID of the role.');
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* {@inheritdoc}
|
98
|
*/
|
99
|
public function add_self_tokens(&$tokens, $item) {
|
100
|
if (!empty($item['role'])) {
|
101
|
$tokens['[' . $this->options['id'] . '-role' . ']'] = $item['role'];
|
102
|
$tokens['[' . $this->options['id'] . '-rid' . ']'] = $item['rid'];
|
103
|
}
|
104
|
}
|
105
|
|
106
|
}
|