1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Flag module tokens support.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements of hook_token_info().
|
10
|
*
|
11
|
* The tokens we provide on generic entities require token module.
|
12
|
*/
|
13
|
function flag_token_info() {
|
14
|
$types = array();
|
15
|
$tokens = array();
|
16
|
|
17
|
// Flag tokens.
|
18
|
$types['flag'] = array(
|
19
|
'name' => t('Flags'),
|
20
|
'description' => t('Tokens related to flag data.'),
|
21
|
'needs-data' => 'flag',
|
22
|
);
|
23
|
$tokens['flag']['name'] = array(
|
24
|
'name' => t('Flag name'),
|
25
|
'description' => t('The flag machine-readable name.'),
|
26
|
);
|
27
|
$tokens['flag']['title'] = array(
|
28
|
'name' => t('Flag title'),
|
29
|
'description' => t('The human-readable flag title.'),
|
30
|
);
|
31
|
|
32
|
// Flagging tokens.
|
33
|
//
|
34
|
// Attached fields are exposed as tokens via some contrib module, but we
|
35
|
// need to expose other fields ourselves. Currently, 'date' is the only such
|
36
|
// field we expose.
|
37
|
$types['flagging'] = array(
|
38
|
'name' => t('Flaggings'),
|
39
|
'description' => t('Tokens related to flaggings.'),
|
40
|
'needs-data' => 'flagging',
|
41
|
);
|
42
|
$tokens['flagging']['date'] = array(
|
43
|
'name' => t('Flagging date'),
|
44
|
'description' => t('The date an item was flagged.'),
|
45
|
'type' => 'date',
|
46
|
);
|
47
|
|
48
|
// Flag action tokens.
|
49
|
$types['flag-action'] = array(
|
50
|
'name' => t('Flag actions'),
|
51
|
'description' => t('Tokens available in response to a flag action being executed by a user.'),
|
52
|
'needs-data' => 'flag-action',
|
53
|
);
|
54
|
$tokens['flag-action']['action'] = array(
|
55
|
'name' => t('Flag action'),
|
56
|
'description' => t('The flagging action taking place, either "flag" or "unflag".'),
|
57
|
);
|
58
|
$tokens['flag-action']['entity-url'] = array(
|
59
|
'name' => t('Flag entity URL'),
|
60
|
'description' => t('The URL of the entity being flagged.'),
|
61
|
);
|
62
|
$tokens['flag-action']['entity-title'] = array(
|
63
|
'name' => t('Flag entity title'),
|
64
|
'description' => t('The title of the entity being flagged.'),
|
65
|
);
|
66
|
$tokens['flag-action']['entity-type'] = array(
|
67
|
'name' => t('Flag entity type'),
|
68
|
'description' => t('The type of entity being flagged, such as <em>node</em> or <em>comment</em>.'),
|
69
|
);
|
70
|
$tokens['flag-action']['entity-id'] = array(
|
71
|
'name' => t('Flag entity ID'),
|
72
|
'description' => t('The ID of entity being flagged, such as a nid or cid.'),
|
73
|
);
|
74
|
$tokens['flag-action']['count'] = array(
|
75
|
'name' => t('Flag count'),
|
76
|
'description' => t('The current count total for this flag.'),
|
77
|
);
|
78
|
|
79
|
// Add tokens for the flag count available at the entity level.
|
80
|
// These require token module because we need its helper data and functions
|
81
|
// to deal with token types that are not the same as the entity types they are
|
82
|
// for (in particular, terms and vocabularies).
|
83
|
if (module_exists('token')) {
|
84
|
$entity_info = entity_get_info();
|
85
|
foreach (flag_get_types() as $flag_type) {
|
86
|
// The flag type is the entity type, but this is not necessarily the same
|
87
|
// as the entity's token type.
|
88
|
$token_type = $entity_info[$flag_type]['token type'];
|
89
|
$flags = flag_get_flags($flag_type);
|
90
|
foreach ($flags as $flag) {
|
91
|
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
|
92
|
'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
|
93
|
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
|
94
|
'flag-type' => $flag_type,
|
95
|
);
|
96
|
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
|
97
|
'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
|
98
|
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
|
99
|
'flag-type' => $flag_type,
|
100
|
);
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
|
105
|
return array(
|
106
|
'types' => $types,
|
107
|
'tokens' => $tokens,
|
108
|
);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Implements hook_tokens().
|
113
|
*/
|
114
|
function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
115
|
$replacements = array();
|
116
|
$sanitize = !empty($options['sanitize']);
|
117
|
$langcode = isset($options['language']) ? $options['language']->language : NULL;
|
118
|
|
119
|
if ($type == 'flag' && !empty($data['flag'])) {
|
120
|
$flag = $data['flag'];
|
121
|
foreach ($tokens as $name => $original) {
|
122
|
switch ($name) {
|
123
|
case 'name':
|
124
|
$replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
|
125
|
break;
|
126
|
|
127
|
case 'title':
|
128
|
$replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
|
129
|
break;
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
elseif ($type == 'flagging' && !empty($data['flagging'])) {
|
134
|
$flagging = $data['flagging'];
|
135
|
foreach ($tokens as $name => $original) {
|
136
|
switch ($name) {
|
137
|
case 'date':
|
138
|
$replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
|
139
|
break;
|
140
|
}
|
141
|
}
|
142
|
if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
|
143
|
$replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
|
144
|
}
|
145
|
}
|
146
|
elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
|
147
|
$action = $data['flag-action'];
|
148
|
foreach ($tokens as $name => $original) {
|
149
|
switch ($name) {
|
150
|
case 'action':
|
151
|
$replacements[$original] = $action->action;
|
152
|
break;
|
153
|
|
154
|
case 'entity-url':
|
155
|
$replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
|
156
|
break;
|
157
|
|
158
|
case 'entity-title':
|
159
|
$replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
|
160
|
break;
|
161
|
|
162
|
case 'entity-type':
|
163
|
$replacements[$original] = $action->entity_type;
|
164
|
break;
|
165
|
|
166
|
case 'entity-id':
|
167
|
$replacements[$original] = $action->entity_id;
|
168
|
break;
|
169
|
|
170
|
case 'count':
|
171
|
$replacements[$original] = $action->count;
|
172
|
break;
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
|
177
|
// We only provide tokens on entity types if we have token module's helper
|
178
|
// methods available.
|
179
|
if (isset($data[$type]) && module_exists('token')) {
|
180
|
$entity_type = token_get_entity_mapping('token', $type);
|
181
|
if ($entity_type && in_array($entity_type, flag_get_types())) {
|
182
|
$flags = flag_get_flags($entity_type);
|
183
|
$object = $data[$type];
|
184
|
foreach ($flags as $flag) {
|
185
|
foreach ($tokens as $name => $original) {
|
186
|
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
|
187
|
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
|
188
|
if ($name == $flag_count_token) {
|
189
|
$replacements[$original] = $flag->get_count($flag->get_entity_id($object));
|
190
|
}
|
191
|
elseif ($name == $flag_link_token) {
|
192
|
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
return $replacements;
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Returns HTML for a tokens browser.
|
203
|
*
|
204
|
* @param $variables
|
205
|
* An associative array containing:
|
206
|
* - types: An array naming the types of tokens to show.
|
207
|
* - global_types: Whether to show global tokens.
|
208
|
*/
|
209
|
function theme_flag_tokens_browser($variables) {
|
210
|
$types = $variables['types'];
|
211
|
$global_types = $variables['global_types'];
|
212
|
|
213
|
if (module_exists('token')) {
|
214
|
return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types));
|
215
|
}
|
216
|
else {
|
217
|
return '<p><em>' . t("Note: You don't have the <a href='@token-url'>Token</a> module installed, so the list of available tokens isn't shown here. You don't have to install <a href='@token-url'>Token</a> to be able to use tokens, but if you have it installed, and enabled, you'll be able to enjoy an interactive tokens browser.", array('@token-url' => 'http://drupal.org/project/token')) . '</em></p>';
|
218
|
}
|
219
|
}
|