forked from backdrop-contrib/matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matomo.tokens.inc
55 lines (47 loc) · 1.41 KB
/
matomo.tokens.inc
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
<?php
/**
* @file
* Builds placeholder replacement tokens for user-related data.
*/
/**
* Implements hook_token_info().
*/
function matomo_token_info() {
$user['matomo-role-names'] = array(
'name' => t('User role names'),
'description' => t('The role names the user account is a member of as comma separated list.'),
'needs-data' => 'user',
);
$user['matomo-role-ids'] = array(
'name' => t('User role ids'),
'description' => t('The role ids the user account is a member of as comma separated list.'),
'needs-data' => 'user',
);
return array(
'tokens' => array('user' => $user),
);
}
/**
* Implements hook_tokens().
*/
function matomo_tokens($type, $tokens, array $data = array(), array $options = array()) {
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'user' && !empty($data['user']->roles)) {
$account = $data['user'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Basic user account information.
case 'matomo-role-names':
$names = implode(',', $account->roles);
$replacements[$original] = $sanitize ? check_plain($names) : $names;
break;
case 'matomo-role-ids':
$ids = implode(',', array_keys($account->roles));
$replacements[$original] = $sanitize ? check_plain($ids) : $ids;
break;
}
}
}
return $replacements;
}