forked from backdrop-contrib/flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.tokens.inc
262 lines (238 loc) · 8.12 KB
/
flag.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* @file
* Flag module tokens support.
*/
/**
* Implements of hook_token_info().
*
* The tokens we provide on generic entities require token module.
*/
function flag_token_info() {
$types = array();
$tokens = array();
// Flag tokens.
$types['flag'] = array(
'name' => t('Flags'),
'description' => t('Tokens related to flag data.'),
'needs-data' => 'flag',
);
$tokens['flag']['name'] = array(
'name' => t('Flag name'),
'description' => t('The flag machine-readable name.'),
);
$tokens['flag']['title'] = array(
'name' => t('Flag title'),
'description' => t('The human-readable flag title.'),
);
// Flagging tokens.
//
// Attached fields are exposed as tokens via some contrib module, but we
// need to expose other fields ourselves. Currently, 'date' is the only such
// field we expose.
$types['flagging'] = array(
'name' => t('Flaggings'),
'description' => t('Tokens related to flaggings.'),
'needs-data' => 'flagging',
);
$tokens['flagging']['date'] = array(
'name' => t('Flagging date'),
'description' => t('The date an item was flagged.'),
'type' => 'date',
);
// Flag action tokens.
$types['flag-action'] = array(
'name' => t('Flag actions'),
'description' => t('Tokens available in response to a flag action being executed by a user.'),
'needs-data' => 'flag-action',
);
$tokens['flag-action']['action'] = array(
'name' => t('Flag action'),
'description' => t('The flagging action taking place, either "flag" or "unflag".'),
);
$tokens['flag-action']['entity-url'] = array(
'name' => t('Flag entity URL'),
'description' => t('The URL of the entity being flagged.'),
);
$tokens['flag-action']['entity-title'] = array(
'name' => t('Flag entity title'),
'description' => t('The title of the entity being flagged.'),
);
$tokens['flag-action']['entity-type'] = array(
'name' => t('Flag entity type'),
'description' => t('The type of entity being flagged, such as <em>node</em> or <em>comment</em>.'),
);
$tokens['flag-action']['entity-id'] = array(
'name' => t('Flag entity ID'),
'description' => t('The ID of entity being flagged, such as a nid or cid.'),
);
$tokens['flag-action']['count'] = array(
'name' => t('Flag count'),
'description' => t('The current count total for this flag.'),
);
// Add tokens for the flag count available at the entity level.
foreach (flag_get_types() as $flag_type) {
// The flag type is the entity type, but this is not necessarily the same
// as the entity's token type.
$token_type = _flag_entity_token_type($flag_type);
$flags = flag_get_flags($flag_type);
foreach ($flags as $flag) {
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
'flag-type' => $flag_type,
);
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
'flag-type' => $flag_type,
);
}
}
return array(
'types' => $types,
'tokens' => $tokens,
);
}
/**
* Implements hook_tokens().
*/
function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
$langcode = isset($options['language']) ? $options['language']->langcode : NULL;
if ($type == 'flag' && !empty($data['flag'])) {
$flag = $data['flag'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'name':
$replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
break;
case 'title':
$replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
break;
}
}
}
elseif ($type == 'flagging' && !empty($data['flagging'])) {
$flagging = $data['flagging'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'date':
$replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
break;
}
}
if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
$replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
}
}
elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
$action = $data['flag-action'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'action':
$replacements[$original] = $action->action;
break;
case 'entity-url':
$replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
break;
case 'entity-title':
$replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
break;
case 'entity-type':
$replacements[$original] = $action->entity_type;
break;
case 'entity-id':
$replacements[$original] = $action->entity_id;
break;
case 'count':
$replacements[$original] = $action->count;
break;
}
}
}
// We only provide tokens on entity types if we have token's helper
// methods available.
if (isset($data[$type])) {
$entity_type = _flag_token_entity_type($type);
if ($entity_type && in_array($entity_type, flag_get_types())) {
$flags = flag_get_flags($entity_type);
$object = $data[$type];
foreach ($flags as $flag) {
foreach ($tokens as $name => $original) {
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
if ($name == $flag_count_token) {
$replacements[$original] = $flag->get_count($flag->get_entity_id($object));
}
elseif ($name == $flag_link_token) {
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
}
}
}
}
}
return $replacements;
}
/**
* Returns HTML for a tokens browser.
*
* @param $variables
* An associative array containing:
* - types: An array naming the types of tokens to show.
* - global_types: Whether to show global tokens.
*/
function theme_flag_tokens_browser($variables) {
$types = $variables['types'];
$global_types = $variables['global_types'];
return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types));
}
/*
* Returns corresponding token type name given entity type name.
*
* @parm $entity_type
* Entity type name.
*
* @return
* Token type name.
*
* Flag module can add flags to any entity, and provide flag-related tokens
* on entities.
*
* Tokens are prefixed by a 'token type' which is generally the entity type
* name, but not always. Backdrop core does not provide a mapping for this.
* Token module can provide this, but if it is not available, we'll make an
* educated guess.
*/
function _flag_entity_token_type($entity_type) {
$info = entity_get_info($entity_type);
// check if token already provided this via hook_entity_info_alter()
if (isset($info['token type'])) {
return $info['token type'];
}
if ($entity_type === 'taxonomy_term') {
return 'term';
}
return $entity_type;
}
/*
* Returns corresponding entity type name given token type name.
*
* @param $token_type
* A token type string
*
* @return
* An entity type name.
*/
function _flag_token_entity_type($token_type) {
// Only concerned about entities available to flag.
$entity_types = flag_fetch_definition();
foreach ($entity_types as $entity_type => $description) {
$info = entity_get_info($entity_type);
if (isset($info['token type']) && $info['token type'] == $token_type) {
return $entity_type;
}
}
return FALSE;
}