1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Enhances the token API in core: adds a browseable UI, missing tokens, etc.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* The maximum depth for token tree recursion.
|
10 |
|
|
*/
|
11 |
|
|
define('TOKEN_MAX_DEPTH', 9);
|
12 |
|
|
|
13 |
|
|
/**
|
14 |
|
|
* Implements hook_help().
|
15 |
|
|
*/
|
16 |
|
|
function token_help($path, $arg) {
|
17 |
|
|
if ($path == 'admin/help#token') {
|
18 |
|
|
if (current_path() != 'admin/help/token') {
|
19 |
|
|
// Because system_modules() executes hook_help() for each module to 'test'
|
20 |
|
|
// if they will return anything, but not actually display it, we want to
|
21 |
|
|
// return a TRUE value if this is not actually the help page.
|
22 |
|
|
return TRUE;
|
23 |
|
|
}
|
24 |
|
|
$output = '<dl>';
|
25 |
|
|
$output .= '<dt>' . t('List of the currently available tokens on this site') . '</dt>';
|
26 |
|
|
$output .= '<dd>' . theme('token_tree', array('token_types' => 'all', 'click_insert' => FALSE, 'show_restricted' => TRUE)) . '</dd>';
|
27 |
|
|
$output .= '</dl>';
|
28 |
|
|
return $output;
|
29 |
|
|
}
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* Implements hook_system_info_alter().
|
34 |
|
|
*
|
35 |
|
|
* Prevent the token_actions module from being enabled since updates may have
|
36 |
|
|
* left the old module files still in the directory.
|
37 |
|
|
*/
|
38 |
|
|
function token_system_info_alter(&$info, $file, $type) {
|
39 |
|
|
if ($type == 'module' && $file->name == 'token_actions') {
|
40 |
|
|
$info['hidden'] = TRUE;
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Return an array of the core modules supported by token.module.
|
46 |
|
|
*/
|
47 |
|
|
function _token_core_supported_modules() {
|
48 |
|
|
return array('book', 'field', 'menu', 'profile');
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* Implements hook_menu().
|
53 |
|
|
*/
|
54 |
|
|
function token_menu() {
|
55 |
|
|
/*$items['token/autocomplete/all/%menu_tail'] = array(
|
56 |
|
|
'page callback' => 'token_autocomplete',
|
57 |
|
|
'access callback' => TRUE,
|
58 |
|
|
'type' => MENU_CALLBACK,
|
59 |
|
|
'file' => 'token.pages.inc',
|
60 |
|
|
);*/
|
61 |
|
|
$items['token/autocomplete/%token_type'] = array(
|
62 |
|
|
'page callback' => 'token_autocomplete_token',
|
63 |
|
|
'page arguments' => array(2),
|
64 |
|
|
'access callback' => TRUE,
|
65 |
|
|
'type' => MENU_CALLBACK,
|
66 |
|
|
'file' => 'token.pages.inc',
|
67 |
|
|
);
|
68 |
|
|
/*$items['token/autocomplete/%token_type/%menu_tail'] = array(
|
69 |
|
|
'page callback' => 'token_autocomplete_token',
|
70 |
|
|
'page arguments' => array(2, 3),
|
71 |
|
|
'access callback' => TRUE,
|
72 |
|
|
'type' => MENU_CALLBACK,
|
73 |
|
|
'file' => 'token.pages.inc',
|
74 |
|
|
);*/
|
75 |
|
|
|
76 |
|
|
$items['token/tree'] = array(
|
77 |
|
|
'page callback' => 'token_page_output_tree',
|
78 |
|
|
'access callback' => TRUE,
|
79 |
|
|
'type' => MENU_CALLBACK,
|
80 |
|
|
'file' => 'token.pages.inc',
|
81 |
|
|
'theme callback' => 'ajax_base_page_theme',
|
82 |
|
|
);
|
83 |
|
|
|
84 |
|
|
// Devel token pages.
|
85 |
|
|
if (module_exists('devel')) {
|
86 |
|
|
$items['node/%node/devel/token'] = array(
|
87 |
|
|
'title' => 'Tokens',
|
88 |
|
|
'page callback' => 'token_devel_token_object',
|
89 |
|
|
'page arguments' => array('node', 1),
|
90 |
|
|
'access arguments' => array('access devel information'),
|
91 |
|
|
'type' => MENU_LOCAL_TASK,
|
92 |
|
|
'file' => 'token.pages.inc',
|
93 |
|
|
'weight' => 5,
|
94 |
|
|
);
|
95 |
|
|
$items['comment/%comment/devel/token'] = array(
|
96 |
|
|
'title' => 'Tokens',
|
97 |
|
|
'page callback' => 'token_devel_token_object',
|
98 |
|
|
'page arguments' => array('comment', 1),
|
99 |
|
|
'access arguments' => array('access devel information'),
|
100 |
|
|
'type' => MENU_LOCAL_TASK,
|
101 |
|
|
'file' => 'token.pages.inc',
|
102 |
|
|
'weight' => 5,
|
103 |
|
|
);
|
104 |
|
|
$items['taxonomy/term/%taxonomy_term/devel/token'] = array(
|
105 |
|
|
'title' => 'Tokens',
|
106 |
|
|
'page callback' => 'token_devel_token_object',
|
107 |
|
|
'page arguments' => array('taxonomy_term', 2),
|
108 |
|
|
'access arguments' => array('access devel information'),
|
109 |
|
|
'type' => MENU_LOCAL_TASK,
|
110 |
|
|
'file' => 'token.pages.inc',
|
111 |
|
|
'weight' => 5,
|
112 |
|
|
);
|
113 |
|
|
$items['user/%user/devel/token'] = array(
|
114 |
|
|
'title' => 'Tokens',
|
115 |
|
|
'page callback' => 'token_devel_token_object',
|
116 |
|
|
'page arguments' => array('user', 1),
|
117 |
|
|
'access arguments' => array('access devel information'),
|
118 |
|
|
'type' => MENU_LOCAL_TASK,
|
119 |
|
|
'file' => 'token.pages.inc',
|
120 |
|
|
'weight' => 5,
|
121 |
|
|
);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
// Admin menu callback to clear token caches.
|
125 |
|
|
$items['token/flush-cache'] = array(
|
126 |
|
|
'page callback' => 'token_flush_cache_callback',
|
127 |
|
|
'access arguments' => array('flush caches'),
|
128 |
|
|
'type' => MENU_CALLBACK,
|
129 |
|
|
'file' => 'token.pages.inc',
|
130 |
|
|
);
|
131 |
|
|
|
132 |
|
|
return $items;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Implements hook_admin_menu_output_alter().
|
137 |
|
|
*/
|
138 |
|
|
function token_admin_menu_output_alter(&$content) {
|
139 |
|
|
$content['icon']['icon']['flush-cache']['token'] = array(
|
140 |
|
|
'#title' => t('Token registry'),
|
141 |
|
|
'#href' => 'token/flush-cache',
|
142 |
|
|
'#options' => array(
|
143 |
|
|
'query' => drupal_get_destination() + array('token' => drupal_get_token('token/flush-cache')),
|
144 |
|
|
),
|
145 |
|
|
);
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
function token_type_load($token_type) {
|
149 |
|
|
$info = token_get_info();
|
150 |
|
|
return isset($info['types'][$token_type]) ? $info['types'][$token_type] : FALSE;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
/**
|
154 |
|
|
* Implements hook_theme().
|
155 |
|
|
*/
|
156 |
|
|
function token_theme() {
|
157 |
|
|
$info['tree_table'] = array(
|
158 |
|
|
'variables' => array(
|
159 |
|
|
'header' => array(),
|
160 |
|
|
'rows' => array(),
|
161 |
|
|
'attributes' => array(),
|
162 |
|
|
'empty' => '',
|
163 |
|
|
'caption' => '',
|
164 |
|
|
),
|
165 |
|
|
'file' => 'token.pages.inc',
|
166 |
|
|
);
|
167 |
|
|
$info['token_tree'] = array(
|
168 |
|
|
'variables' => array(
|
169 |
|
|
'token_types' => array(),
|
170 |
|
|
'global_types' => TRUE,
|
171 |
|
|
'click_insert' => TRUE,
|
172 |
|
|
'show_restricted' => FALSE,
|
173 |
|
|
'recursion_limit' => 3,
|
174 |
|
|
'dialog' => FALSE,
|
175 |
|
|
),
|
176 |
|
|
'file' => 'token.pages.inc',
|
177 |
|
|
);
|
178 |
|
|
$info['token_tree_link'] = array(
|
179 |
|
|
'variables' => array(
|
180 |
|
|
'text' => NULL,
|
181 |
|
|
'options' => array(),
|
182 |
|
|
'dialog' => TRUE,
|
183 |
|
|
),
|
184 |
|
|
'file' => 'token.pages.inc',
|
185 |
|
|
);
|
186 |
|
|
|
187 |
|
|
return $info;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/**
|
191 |
|
|
* Implements hook_library().
|
192 |
|
|
*/
|
193 |
|
|
function token_library() {
|
194 |
|
|
// jQuery treeTable plugin.
|
195 |
|
|
$libraries['treeTable'] = array(
|
196 |
|
|
'title' => 'jQuery treeTable',
|
197 |
|
|
'website' => 'http://plugins.jquery.com/project/treetable',
|
198 |
|
|
'version' => '2.3.0',
|
199 |
|
|
'js' => array(
|
200 |
|
|
drupal_get_path('module', 'token') . '/jquery.treeTable.js' => array(),
|
201 |
|
|
),
|
202 |
|
|
'css' => array(
|
203 |
|
|
drupal_get_path('module', 'token') . '/jquery.treeTable.css' => array(),
|
204 |
|
|
),
|
205 |
|
|
);
|
206 |
|
|
|
207 |
|
|
$libraries['dialog'] = array(
|
208 |
|
|
'title' => 'Token dialog',
|
209 |
|
|
'version' => '1.0',
|
210 |
|
|
'js' => array(
|
211 |
|
|
drupal_get_path('module', 'token') . '/token.js' => array(),
|
212 |
|
|
),
|
213 |
|
|
'dependencies' => array(
|
214 |
|
|
array('system', 'ui.dialog'),
|
215 |
|
|
),
|
216 |
|
|
);
|
217 |
|
|
|
218 |
|
|
return $libraries;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
/**
|
222 |
|
|
* Implements hook_form_alter().
|
223 |
|
|
*
|
224 |
|
|
* Adds a submit handler to forms which could affect the tokens available on
|
225 |
|
|
* the site.
|
226 |
|
|
*/
|
227 |
|
|
function token_form_alter(&$form, $form_state, $form_id) {
|
228 |
|
|
switch ($form_id) {
|
229 |
|
|
// Profile field forms.
|
230 |
|
|
case 'profile_field_form':
|
231 |
|
|
case 'profile_field_delete':
|
232 |
|
|
// User picture form.
|
233 |
|
|
case 'user_admin_settings':
|
234 |
|
|
// System date type form.
|
235 |
|
|
// @todo Remove when http://drupal.org/node/1173706 is fixed.
|
236 |
|
|
case 'system_add_date_format_type_form':
|
237 |
|
|
case 'system_delete_date_format_type_form':
|
238 |
|
|
$form += array('#submit' => array());
|
239 |
|
|
array_unshift($form['#submit'], 'token_clear_cache');
|
240 |
|
|
break;
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Implements hook_block_view_alter().
|
246 |
|
|
*/
|
247 |
|
|
function token_block_view_alter(&$data, $block) {
|
248 |
|
|
if (!empty($block->title) && $block->title != '<none>') {
|
249 |
|
|
// Perform unsanitized token replacement since _block_render_blocks() will
|
250 |
|
|
// call check_plain() on $block->title.
|
251 |
|
|
$block->title = token_replace($block->title, array(), array('sanitize' => FALSE));
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
/**
|
256 |
|
|
* Implements hook_form_FORM_ID_alter().
|
257 |
|
|
*/
|
258 |
|
|
function token_form_block_add_block_form_alter(&$form, $form_state) {
|
259 |
|
|
token_form_block_admin_configure_alter($form, $form_state);
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
/**
|
263 |
|
|
* Implements hook_form_FORM_ID_alter().
|
264 |
|
|
*/
|
265 |
|
|
function token_form_block_admin_configure_alter(&$form, $form_state) {
|
266 |
|
|
$form['settings']['title']['#description'] .= ' ' . t('This field supports tokens.');
|
267 |
|
|
// @todo Figure out why this token validation does not seem to be working here.
|
268 |
|
|
$form['settings']['title']['#element_validate'][] = 'token_element_validate';
|
269 |
|
|
$form['settings']['title'] += array('#token_types' => array());
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/**
|
273 |
|
|
* Implements hook_widget_form_alter().
|
274 |
|
|
*/
|
275 |
|
|
function token_field_widget_form_alter(&$element, &$form_state, $context) {
|
276 |
|
|
if (!empty($element['#description']) && is_string($element['#description'])) {
|
277 |
|
|
$element['#description'] = filter_xss_admin(token_replace($element['#description']));
|
278 |
|
|
}
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
/**
|
282 |
|
|
* Implements hook_field_info_alter().
|
283 |
|
|
*/
|
284 |
|
|
function token_field_info_alter(&$info) {
|
285 |
|
|
$defaults = array(
|
286 |
|
|
'taxonomy_term_reference' => 'taxonomy_term_reference_plain',
|
287 |
|
|
'number_integer' => 'number_unformatted',
|
288 |
|
|
'number_decimal' => 'number_unformatted',
|
289 |
|
|
'number_float' => 'number_unformatted',
|
290 |
|
|
'file' => 'file_url_plain',
|
291 |
|
|
'image' => 'file_url_plain',
|
292 |
|
|
'text' => 'text_default',
|
293 |
|
|
'text_long' => 'text_default',
|
294 |
|
|
'text_with_summary' => 'text_default',
|
295 |
|
|
'list_integer' => 'list_default',
|
296 |
|
|
'list_float' => 'list_default',
|
297 |
|
|
'list_text' => 'list_default',
|
298 |
|
|
'list_boolean' => 'list_default',
|
299 |
|
|
);
|
300 |
|
|
foreach ($defaults as $field_type => $default_token_formatter) {
|
301 |
|
|
if (isset($info[$field_type])) {
|
302 |
|
|
$info[$field_type] += array('default_token_formatter' => $default_token_formatter);
|
303 |
|
|
}
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
/**
|
308 |
|
|
* Implements hook_field_display_alter().
|
309 |
|
|
*/
|
310 |
|
|
function token_field_display_alter(&$display, $context) {
|
311 |
|
|
if ($context['view_mode'] == 'token') {
|
312 |
|
|
$view_mode_settings = field_view_mode_settings($context['instance']['entity_type'], $context['instance']['bundle']);
|
313 |
|
|
// If the token view mode fell back to the 'default' view mode, then
|
314 |
|
|
// use the default token formatter.
|
315 |
|
|
if (empty($view_mode_settings[$context['view_mode']]['custom_settings'])) {
|
316 |
|
|
$field_type_info = field_info_field_types($context['field']['type']);
|
317 |
|
|
|
318 |
|
|
// If the field has specified a specific formatter to be used by default
|
319 |
|
|
// with tokens, use that, otherwise use the default formatter.
|
320 |
|
|
$formatter = !empty($field_type_info['default_token_formatter']) ? $field_type_info['default_token_formatter'] : $field_type_info['default_formatter'];
|
321 |
|
|
|
322 |
|
|
// Now that we have a formatter, fill in all the settings.
|
323 |
|
|
$display['type'] = $formatter;
|
324 |
|
|
$formatter_info = field_info_formatter_types($formatter);
|
325 |
|
|
$display['settings'] = isset($formatter_info['settings']) ? $formatter_info['settings'] : array();
|
326 |
|
|
$display['settings']['label'] = 'hidden';
|
327 |
|
|
$display['module'] = $formatter_info['module'];
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
/**
|
333 |
|
|
* Implements hook_field_create_instance().
|
334 |
|
|
*/
|
335 |
|
|
function token_field_create_instance($instance) {
|
336 |
|
|
token_clear_cache();
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
/**
|
340 |
|
|
* Implements hook_field_update_instance().
|
341 |
|
|
*/
|
342 |
|
|
function token_field_update_instance($instance) {
|
343 |
|
|
token_clear_cache();
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
/**
|
347 |
|
|
* Implements hook_field_delete_instance().
|
348 |
|
|
*/
|
349 |
|
|
function token_field_delete_instance($instance) {
|
350 |
|
|
token_clear_cache();
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
/**
|
354 |
|
|
* Clear token caches and static variables.
|
355 |
|
|
*/
|
356 |
|
|
function token_clear_cache() {
|
357 |
|
|
if (db_table_exists('cache_token')) {
|
358 |
|
|
cache_clear_all('*', 'cache_token', TRUE);
|
359 |
|
|
}
|
360 |
|
|
drupal_static_reset('token_get_info');
|
361 |
|
|
drupal_static_reset('token_get_global_token_types');
|
362 |
|
|
drupal_static_reset('token_get_entity_mapping');
|
363 |
|
|
drupal_static_reset('token_build_tree');
|
364 |
|
|
drupal_static_reset('_token_profile_fields');
|
365 |
|
|
drupal_static_reset('token_menu_link_load');
|
366 |
|
|
drupal_static_reset('token_book_link_load');
|
367 |
|
|
drupal_static_reset('token_node_menu_link_load');
|
368 |
|
|
drupal_static_reset('_token_field_info');
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
/**
|
372 |
|
|
* Return an array of entity type to token type mappings.
|
373 |
|
|
*
|
374 |
|
|
* Why do we need this? Because when the token API was moved to core we did not
|
375 |
|
|
* re-use the entity type as the base name for taxonomy terms and vocabulary
|
376 |
|
|
* tokens.
|
377 |
|
|
*
|
378 |
|
|
* @see token_entity_info_alter()
|
379 |
|
|
* @see http://drupal.org/node/737726
|
380 |
|
|
*/
|
381 |
|
|
function token_get_entity_mapping($value_type = 'token', $value = NULL, $fallback = FALSE) {
|
382 |
|
|
$mapping = &drupal_static(__FUNCTION__, array());
|
383 |
|
|
|
384 |
|
|
if (empty($mapping)) {
|
385 |
|
|
foreach (entity_get_info() as $entity_type => $info) {
|
386 |
|
|
$mapping[$entity_type] = !empty($info['token type']) ? $info['token type'] : $entity_type;
|
387 |
|
|
}
|
388 |
|
|
// Allow modules to alter the mapping array.
|
389 |
|
|
drupal_alter('token_entity_mapping', $mapping);
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
if (!isset($value)) {
|
393 |
|
|
return $value_type == 'token' ? array_flip($mapping) : $mapping;
|
394 |
|
|
}
|
395 |
|
|
elseif ($value_type == 'token') {
|
396 |
|
|
$return = array_search($value, $mapping);
|
397 |
|
|
return $return !== FALSE ? $return : ($fallback ? $value : FALSE);
|
398 |
|
|
}
|
399 |
|
|
elseif ($value_type == 'entity') {
|
400 |
|
|
return isset($mapping[$value]) ? $mapping[$value] : ($fallback ? $value : FALSE);
|
401 |
|
|
}
|
402 |
|
|
}
|
403 |
|
|
|
404 |
|
|
/**
|
405 |
|
|
* Implements hook_entity_info_alter().
|
406 |
|
|
*
|
407 |
|
|
* Because some token types to do not match their entity type names, we have to
|
408 |
|
|
* map them to the proper type. This is purely for other modules' benefit.
|
409 |
|
|
*
|
410 |
|
|
* @see token_get_entity_mapping()
|
411 |
|
|
* @see http://drupal.org/node/737726
|
412 |
|
|
*/
|
413 |
|
|
function token_entity_info_alter(&$info) {
|
414 |
|
|
foreach (array_keys($info) as $entity_type) {
|
415 |
|
|
// Add a token view mode if it does not already exist.
|
416 |
|
|
if (!empty($info[$entity_type]['view modes']) && !isset($info[$entity_type]['view modes']['token'])) {
|
417 |
|
|
$info[$entity_type]['view modes']['token'] = array(
|
418 |
|
|
'label' => t('Tokens'),
|
419 |
|
|
'custom settings' => FALSE,
|
420 |
|
|
);
|
421 |
|
|
}
|
422 |
|
|
|
423 |
|
|
if (!empty($info[$entity_type]['token type'])) {
|
424 |
|
|
// If the entity's token type is already defined, great!
|
425 |
|
|
continue;
|
426 |
|
|
}
|
427 |
|
|
|
428 |
|
|
// Fill in default token types for entities.
|
429 |
|
|
switch ($entity_type) {
|
430 |
|
|
case 'taxonomy_term':
|
431 |
|
|
case 'taxonomy_vocabulary':
|
432 |
|
|
// Stupid taxonomy token types...
|
433 |
|
|
$info[$entity_type]['token type'] = str_replace('taxonomy_', '', $entity_type);
|
434 |
|
|
break;
|
435 |
|
|
default:
|
436 |
|
|
// By default the token type is the same as the entity type.
|
437 |
|
|
$info[$entity_type]['token type'] = $entity_type;
|
438 |
|
|
break;
|
439 |
|
|
}
|
440 |
|
|
}
|
441 |
|
|
}
|
442 |
|
|
|
443 |
|
|
/**
|
444 |
|
|
* Implements hook_module_implements_alter().
|
445 |
|
|
*
|
446 |
|
|
* Adds missing token support for core modules.
|
447 |
|
|
*/
|
448 |
|
|
function token_module_implements_alter(&$implementations, $hook) {
|
449 |
|
|
module_load_include('inc', 'token', 'token.tokens');
|
450 |
|
|
|
451 |
|
|
if ($hook == 'tokens' || $hook == 'token_info' || $hook == 'token_info_alter' || $hook == 'tokens_alter') {
|
452 |
|
|
foreach (_token_core_supported_modules() as $module) {
|
453 |
|
|
if (module_exists($module) && function_exists($module . '_' . $hook)) {
|
454 |
|
|
$implementations[$module] = FALSE;
|
455 |
|
|
}
|
456 |
|
|
}
|
457 |
|
|
// Move token.module to get included first since it is responsible for
|
458 |
|
|
// other modules.
|
459 |
|
|
unset($implementations['token']);
|
460 |
|
|
$implementations = array_merge(array('token' => 'tokens'), $implementations);
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
|
464 |
|
|
/**
|
465 |
|
|
* Implements hook_flush_caches().
|
466 |
|
|
*/
|
467 |
|
|
function token_flush_caches() {
|
468 |
|
|
if (db_table_exists('cache_token')) {
|
469 |
|
|
return array('cache_token');
|
470 |
|
|
}
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
/**
|
474 |
|
|
* Retrieve, sort, store token info from the cache.
|
475 |
|
|
*
|
476 |
|
|
* @param $token_type
|
477 |
|
|
* The optional token type that if specified will return
|
478 |
|
|
* $info['types'][$token_type].
|
479 |
|
|
* @param $token
|
480 |
|
|
* The optional token name if specified will return
|
481 |
|
|
* $info['tokens'][$token_type][$token].
|
482 |
|
|
*
|
483 |
|
|
* @return
|
484 |
|
|
* An array of all token information from hook_token_info(), or the array
|
485 |
|
|
* of a token type or specific token.
|
486 |
|
|
*
|
487 |
|
|
* @see hook_token_info()
|
488 |
|
|
* @see hook_token_info_alter()
|
489 |
|
|
*/
|
490 |
|
|
function token_get_info($token_type = NULL, $token = NULL) {
|
491 |
|
|
global $language;
|
492 |
|
|
|
493 |
|
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
494 |
|
|
static $drupal_static_fast;
|
495 |
|
|
if (!isset($drupal_static_fast)) {
|
496 |
|
|
$drupal_static_fast['token_info'] = &drupal_static(__FUNCTION__);
|
497 |
|
|
}
|
498 |
|
|
$token_info = &$drupal_static_fast['token_info'];
|
499 |
|
|
|
500 |
|
|
if (empty($token_info)) {
|
501 |
|
|
$cid = "info:{$language->language}";
|
502 |
|
|
|
503 |
|
|
if ($cache = cache_get($cid, 'cache_token')) {
|
504 |
|
|
$token_info = $cache->data;
|
505 |
|
|
}
|
506 |
|
|
else {
|
507 |
|
|
$token_info = token_info();
|
508 |
|
|
|
509 |
|
|
foreach (array_keys($token_info['types']) as $type_key) {
|
510 |
|
|
if (isset($token_info['types'][$type_key]['type'])) {
|
511 |
|
|
$base_type = $token_info['types'][$type_key]['type'];
|
512 |
|
|
// If this token type extends another token type, then merge in
|
513 |
|
|
// the base token type's tokens.
|
514 |
|
|
if (isset($token_info['tokens'][$base_type])) {
|
515 |
|
|
$token_info['tokens'] += array($type_key => array());
|
516 |
|
|
$token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
|
517 |
|
|
}
|
518 |
|
|
}
|
519 |
|
|
else {
|
520 |
|
|
// Add a 'type' value to each token type so we can properly use
|
521 |
|
|
// token_type_load().
|
522 |
|
|
$token_info['types'][$type_key]['type'] = $type_key;
|
523 |
|
|
}
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
// Pre-sort tokens.
|
527 |
|
|
uasort($token_info['types'], 'token_asort_tokens');
|
528 |
|
|
foreach (array_keys($token_info['tokens']) as $type) {
|
529 |
|
|
uasort($token_info['tokens'][$type], 'token_asort_tokens');
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
// Store info in cache for future use.
|
533 |
|
|
cache_set($cid, $token_info, 'cache_token');
|
534 |
|
|
}
|
535 |
|
|
}
|
536 |
|
|
|
537 |
|
|
if (isset($token_type) && isset($token)) {
|
538 |
|
|
return isset($token_info['tokens'][$token_type][$token]) ? $token_info['tokens'][$token_type][$token] : NULL;
|
539 |
|
|
}
|
540 |
|
|
elseif (isset($token_type)) {
|
541 |
|
|
return isset($token_info['types'][$token_type]) ? $token_info['types'][$token_type] : NULL;
|
542 |
|
|
}
|
543 |
|
|
else {
|
544 |
|
|
return $token_info;
|
545 |
|
|
}
|
546 |
|
|
}
|
547 |
|
|
|
548 |
|
|
/**
|
549 |
|
|
* Return the module responsible for a token.
|
550 |
|
|
*
|
551 |
|
|
* @param string $type
|
552 |
|
|
* The token type.
|
553 |
|
|
* @param string $name
|
554 |
|
|
* The token name.
|
555 |
|
|
*
|
556 |
|
|
* @return mixed
|
557 |
|
|
* The value of $info['tokens'][$type][$name]['module'] from token_get_info(),
|
558 |
|
|
* or NULL if the value does not exist.
|
559 |
|
|
*/
|
560 |
|
|
function _token_module($type, $name) {
|
561 |
|
|
$token_info = token_get_info($type, $name);
|
562 |
|
|
return isset($token_info['module']) ? $token_info['module'] : NULL;
|
563 |
|
|
}
|
564 |
|
|
|
565 |
|
|
/**
|
566 |
|
|
* uasort() callback to sort tokens by the 'name' property.
|
567 |
|
|
*/
|
568 |
|
|
function token_asort_tokens($token_a, $token_b) {
|
569 |
|
|
return strnatcmp($token_a['name'], $token_b['name']);
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
/**
|
573 |
|
|
* Get a list of token types that can be used without any context (global).
|
574 |
|
|
*
|
575 |
|
|
* @return
|
576 |
|
|
* An array of global token types.
|
577 |
|
|
*/
|
578 |
|
|
function token_get_global_token_types() {
|
579 |
|
|
$global_types = &drupal_static(__FUNCTION__, array());
|
580 |
|
|
|
581 |
|
|
if (empty($global_types)) {
|
582 |
|
|
$token_info = token_get_info();
|
583 |
|
|
foreach ($token_info['types'] as $type => $type_info) {
|
584 |
|
|
// If the token types has not specified that 'needs-data' => TRUE, then
|
585 |
|
|
// it is a global token type that will always be replaced in any context.
|
586 |
|
|
if (empty($type_info['needs-data'])) {
|
587 |
|
|
$global_types[] = $type;
|
588 |
|
|
}
|
589 |
|
|
}
|
590 |
|
|
}
|
591 |
|
|
|
592 |
|
|
return $global_types;
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
/**
|
596 |
|
|
* Validate an tokens in raw text based on possible contexts.
|
597 |
|
|
*
|
598 |
|
|
* @param $value
|
599 |
|
|
* A string with the raw text containing the raw tokens, or an array of
|
600 |
|
|
* tokens from token_scan().
|
601 |
|
|
* @param $tokens
|
602 |
|
|
* An array of token types that will be used when token replacement is
|
603 |
|
|
* performed.
|
604 |
|
|
* @return
|
605 |
|
|
* An array with the invalid tokens in their original raw forms.
|
606 |
|
|
*/
|
607 |
|
|
function token_get_invalid_tokens_by_context($value, $valid_types = array()) {
|
608 |
|
|
if (in_array('all', $valid_types)) {
|
609 |
|
|
$info = token_get_info();
|
610 |
|
|
$valid_types = array_keys($info['types']);
|
611 |
|
|
}
|
612 |
|
|
else {
|
613 |
|
|
// Add the token types that are always valid in global context.
|
614 |
|
|
$valid_types = array_merge($valid_types, token_get_global_token_types());
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
$invalid_tokens = array();
|
618 |
|
|
$value_tokens = is_string($value) ? token_scan($value) : $value;
|
619 |
|
|
|
620 |
|
|
foreach ($value_tokens as $type => $tokens) {
|
621 |
|
|
if (!in_array($type, $valid_types)) {
|
622 |
|
|
// If the token type is not a valid context, its tokens are invalid.
|
623 |
|
|
$invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
|
624 |
|
|
}
|
625 |
|
|
else {
|
626 |
|
|
// Check each individual token for validity.
|
627 |
|
|
$invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($type, $tokens));
|
628 |
|
|
}
|
629 |
|
|
}
|
630 |
|
|
|
631 |
|
|
array_unique($invalid_tokens);
|
632 |
|
|
return $invalid_tokens;
|
633 |
|
|
}
|
634 |
|
|
|
635 |
|
|
/**
|
636 |
|
|
* Validate an array of tokens based on their token type.
|
637 |
|
|
*
|
638 |
|
|
* @param $type
|
639 |
|
|
* The type of tokens to validate (e.g. 'node', etc.)
|
640 |
|
|
* @param $tokens
|
641 |
|
|
* A keyed array of tokens, and their original raw form in the source text.
|
642 |
|
|
* @return
|
643 |
|
|
* An array with the invalid tokens in their original raw forms.
|
644 |
|
|
*/
|
645 |
|
|
function token_get_invalid_tokens($type, $tokens) {
|
646 |
|
|
$token_info = token_get_info();
|
647 |
|
|
$invalid_tokens = array();
|
648 |
|
|
|
649 |
|
|
foreach ($tokens as $token => $full_token) {
|
650 |
|
|
// Split token up if it has chains.
|
651 |
|
|
$parts = explode(':', $token, 2);
|
652 |
|
|
|
653 |
|
|
if (!isset($token_info['tokens'][$type][$parts[0]])) {
|
654 |
|
|
// This is an invalid token (not defined).
|
655 |
|
|
$invalid_tokens[] = $full_token;
|
656 |
|
|
}
|
657 |
|
|
elseif (count($parts) == 2) {
|
658 |
|
|
$sub_token_info = $token_info['tokens'][$type][$parts[0]];
|
659 |
|
|
if (!empty($sub_token_info['dynamic'])) {
|
660 |
|
|
// If this token has been flagged as a dynamic token, skip it.
|
661 |
|
|
continue;
|
662 |
|
|
}
|
663 |
|
|
elseif (empty($sub_token_info['type'])) {
|
664 |
|
|
// If the token has chains, but does not support it, it is invalid.
|
665 |
|
|
$invalid_tokens[] = $full_token;
|
666 |
|
|
}
|
667 |
|
|
else {
|
668 |
|
|
// Resursively check the chained tokens.
|
669 |
|
|
$sub_tokens = token_find_with_prefix(array($token => $full_token), $parts[0]);
|
670 |
|
|
$invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($sub_token_info['type'], $sub_tokens));
|
671 |
|
|
}
|
672 |
|
|
}
|
673 |
|
|
}
|
674 |
|
|
|
675 |
|
|
return $invalid_tokens;
|
676 |
|
|
}
|
677 |
|
|
|
678 |
|
|
/**
|
679 |
|
|
* Validate a form element that should have tokens in it.
|
680 |
|
|
*
|
681 |
|
|
* Form elements that want to add this validation should have the #token_types
|
682 |
|
|
* parameter defined.
|
683 |
|
|
*
|
684 |
|
|
* For example:
|
685 |
|
|
* @code
|
686 |
|
|
* $form['my_node_text_element'] = array(
|
687 |
|
|
* '#type' => 'textfield',
|
688 |
|
|
* '#title' => t('Some text to token-ize that has a node context.'),
|
689 |
|
|
* '#default_value' => 'The title of this node is [node:title].',
|
690 |
|
|
* '#element_validate' => array('token_element_validate'),
|
691 |
|
|
* '#token_types' => array('node'),
|
692 |
|
|
* '#min_tokens' => 1,
|
693 |
|
|
* '#max_tokens' => 10,
|
694 |
|
|
* );
|
695 |
|
|
* @endcode
|
696 |
|
|
*/
|
697 |
|
|
function token_element_validate(&$element, &$form_state) {
|
698 |
|
|
$value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
|
699 |
|
|
|
700 |
|
|
if (!drupal_strlen($value)) {
|
701 |
|
|
// Empty value needs no further validation since the element should depend
|
702 |
|
|
// on using the '#required' FAPI property.
|
703 |
|
|
return $element;
|
704 |
|
|
}
|
705 |
|
|
|
706 |
|
|
$tokens = token_scan($value);
|
707 |
|
|
$title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
|
708 |
|
|
// @todo Find old Drupal 6 style tokens and add them to invalid tokens.
|
709 |
|
|
|
710 |
|
|
// Validate if an element must have a minimum number of tokens.
|
711 |
|
|
if (isset($element['#min_tokens']) && count($tokens) < $element['#min_tokens']) {
|
712 |
|
|
// @todo Change this error message to include the minimum number.
|
713 |
|
|
$error = format_plural($element['#min_tokens'], 'The %element-title cannot contain fewer than one token.', 'The %element-title must contain at least @count tokens.', array('%element-title' => $title));
|
714 |
|
|
form_error($element, $error);
|
715 |
|
|
}
|
716 |
|
|
|
717 |
|
|
// Validate if an element must have a maximum number of tokens.
|
718 |
|
|
if (isset($element['#max_tokens']) && count($tokens) > $element['#max_tokens']) {
|
719 |
|
|
// @todo Change this error message to include the maximum number.
|
720 |
|
|
$error = format_plural($element['#max_tokens'], 'The %element-title must contain as most one token.', 'The %element-title must contain at most @count tokens.', array('%element-title' => $title));
|
721 |
|
|
form_error($element, $error);
|
722 |
|
|
}
|
723 |
|
|
|
724 |
|
|
// Check if the field defines specific token types.
|
725 |
|
|
if (isset($element['#token_types'])) {
|
726 |
|
|
$invalid_tokens = token_get_invalid_tokens_by_context($tokens, $element['#token_types']);
|
727 |
|
|
if ($invalid_tokens) {
|
728 |
|
|
form_error($element, t('The %element-title is using the following invalid tokens: @invalid-tokens.', array('%element-title' => $title, '@invalid-tokens' => implode(', ', $invalid_tokens))));
|
729 |
|
|
}
|
730 |
|
|
}
|
731 |
|
|
|
732 |
|
|
return $element;
|
733 |
|
|
}
|
734 |
|
|
|
735 |
|
|
/**
|
736 |
|
|
* Deprecated. Use token_element_validate() instead.
|
737 |
|
|
*/
|
738 |
|
|
function token_element_validate_token_context(&$element, &$form_state) {
|
739 |
|
|
return token_element_validate($element, $form_state);
|
740 |
|
|
}
|
741 |
|
|
|
742 |
|
|
/**
|
743 |
|
|
* Implements hook_form_FORM_ID_alter().
|
744 |
|
|
*/
|
745 |
|
|
function token_form_field_ui_field_edit_form_alter(&$form, $form_state) {
|
746 |
|
|
if (!isset($form['instance']) || !empty($form['#field']['locked'])) {
|
747 |
|
|
return;
|
748 |
|
|
}
|
749 |
|
|
|
750 |
|
|
if (($form['#field']['type'] == 'file' || $form['#field']['type'] == 'image') && isset($form['instance']['settings']['file_directory']) && !module_exists('filefield_paths')) {
|
751 |
|
|
// GAH! We can only support global tokens in the upload file directory path.
|
752 |
|
|
$form['instance']['settings']['file_directory']['#element_validate'][] = 'token_element_validate';
|
753 |
|
|
$form['instance']['settings']['file_directory'] += array('#token_types' => array());
|
754 |
|
|
$form['instance']['settings']['file_directory']['#description'] .= ' ' . t('This field supports tokens.');
|
755 |
|
|
}
|
756 |
|
|
|
757 |
|
|
// Note that the description is tokenized via token_field_widget_form_alter().
|
758 |
|
|
$form['instance']['description']['#description'] .= '<br />' . t('This field supports tokens.');
|
759 |
|
|
$form['instance']['description']['#element_validate'][] = 'token_element_validate';
|
760 |
|
|
$form['instance']['description'] += array('#token_types' => array());
|
761 |
|
|
|
762 |
|
|
$form['instance']['settings']['token_tree'] = array(
|
763 |
|
|
'#theme' => 'token_tree',
|
764 |
|
|
'#token_types' => array(),
|
765 |
|
|
'#dialog' => TRUE,
|
766 |
|
|
'#weight' => $form['instance']['description']['#weight'] + 0.5,
|
767 |
|
|
);
|
768 |
|
|
}
|
769 |
|
|
|
770 |
|
|
/**
|
771 |
|
|
* Implements hook_form_FORM_ID_alter().
|
772 |
|
|
*
|
773 |
|
|
* Alters the configure action form to add token context validation and
|
774 |
|
|
* adds the token tree for a better token UI and selection.
|
775 |
|
|
*/
|
776 |
|
|
function token_form_system_actions_configure_alter(&$form, $form_state) {
|
777 |
|
|
$action = actions_function_lookup($form['actions_action']['#value']);
|
778 |
|
|
|
779 |
|
|
switch ($action) {
|
780 |
|
|
case 'system_message_action':
|
781 |
|
|
case 'system_send_email_action':
|
782 |
|
|
case 'system_goto_action':
|
783 |
|
|
$form['token_tree'] = array(
|
784 |
|
|
'#theme' => 'token_tree',
|
785 |
|
|
'#token_types' => 'all',
|
786 |
|
|
'#dialog' => TRUE,
|
787 |
|
|
'#weight' => 100,
|
788 |
|
|
);
|
789 |
|
|
// @todo Add token validation to the action fields that can use tokens.
|
790 |
|
|
break;
|
791 |
|
|
}
|
792 |
|
|
}
|
793 |
|
|
|
794 |
|
|
/**
|
795 |
|
|
* Implements hook_form_FORM_ID_alter().
|
796 |
|
|
*
|
797 |
|
|
* Alters the user e-mail fields to add token context validation and
|
798 |
|
|
* adds the token tree for a better token UI and selection.
|
799 |
|
|
*/
|
800 |
|
|
function token_form_user_admin_settings_alter(&$form, &$form_state) {
|
801 |
|
|
$email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
|
802 |
|
|
|
803 |
|
|
foreach (element_children($form) as $key) {
|
804 |
|
|
$element = &$form[$key];
|
805 |
|
|
|
806 |
|
|
// Remove the crummy default token help text.
|
807 |
|
|
if (!empty($element['#description'])) {
|
808 |
|
|
$element['#description'] = trim(str_replace($email_token_help, t('The list of available tokens that can be used in e-mails is provided below.'), $element['#description']));
|
809 |
|
|
}
|
810 |
|
|
|
811 |
|
|
switch ($key) {
|
812 |
|
|
case 'email_admin_created':
|
813 |
|
|
case 'email_pending_approval':
|
814 |
|
|
case 'email_no_approval_required':
|
815 |
|
|
case 'email_password_reset':
|
816 |
|
|
case 'email_cancel_confirm':
|
817 |
|
|
// Do nothing, but allow execution to continue.
|
818 |
|
|
break;
|
819 |
|
|
case 'email_activated':
|
820 |
|
|
case 'email_blocked':
|
821 |
|
|
case 'email_canceled':
|
822 |
|
|
// These fieldsets have their e-mail elements inside a 'settings'
|
823 |
|
|
// sub-element, so switch to that element instead.
|
824 |
|
|
$element = &$form[$key]['settings'];
|
825 |
|
|
break;
|
826 |
|
|
default:
|
827 |
|
|
continue 2;
|
828 |
|
|
}
|
829 |
|
|
|
830 |
|
|
foreach (element_children($element) as $sub_key) {
|
831 |
|
|
if (!isset($element[$sub_key]['#type'])) {
|
832 |
|
|
continue;
|
833 |
|
|
}
|
834 |
|
|
elseif ($element[$sub_key]['#type'] == 'textfield' && substr($sub_key, -8) === '_subject') {
|
835 |
|
|
// Add validation to subject textfields.
|
836 |
|
|
$element[$sub_key]['#element_validate'][] = 'token_element_validate';
|
837 |
|
|
$element[$sub_key] += array('#token_types' => array('user'));
|
838 |
|
|
}
|
839 |
|
|
elseif ($element[$sub_key]['#type'] == 'textarea' && substr($sub_key, -5) === '_body') {
|
840 |
|
|
// Add validation to body textareas.
|
841 |
|
|
$element[$sub_key]['#element_validate'][] = 'token_element_validate';
|
842 |
|
|
$element[$sub_key] += array('#token_types' => array('user'));
|
843 |
|
|
}
|
844 |
|
|
}
|
845 |
|
|
}
|
846 |
|
|
|
847 |
|
|
// Add the token tree UI.
|
848 |
|
|
$form['email']['token_tree'] = array(
|
849 |
|
|
'#theme' => 'token_tree',
|
850 |
|
|
'#token_types' => array('user'),
|
851 |
|
|
'#show_restricted' => TRUE,
|
852 |
|
|
'#dialog' => TRUE,
|
853 |
|
|
'#weight' => 90,
|
854 |
|
|
);
|
855 |
|
|
}
|
856 |
|
|
|
857 |
|
|
/**
|
858 |
|
|
* Build a tree array of tokens used for themeing or information.
|
859 |
|
|
*
|
860 |
|
|
* @param $token_type
|
861 |
|
|
* The token type.
|
862 |
|
|
* @param $flat_tree
|
863 |
|
|
* A boolean if TRUE will only make a flat array of tokens, otherwise
|
864 |
|
|
* child tokens will be inside the 'children' parameter of a token.
|
865 |
|
|
* @param $show_restricted
|
866 |
|
|
* A boolean if TRUE will show restricted tokens. Otherwise they will be
|
867 |
|
|
* hidden. Default is FALSE.
|
868 |
|
|
* @param $recursion_limit
|
869 |
|
|
* An integer with the maximum number of token levels to recurse.
|
870 |
|
|
* @param $parents
|
871 |
|
|
* An optional array with the current parents of the tokens.
|
872 |
|
|
*/
|
873 |
|
|
function token_build_tree($token_type, array $options = array()) {
|
874 |
|
|
global $language;
|
875 |
|
|
|
876 |
|
|
// Static cache of already built token trees.
|
877 |
|
|
$trees = &drupal_static(__FUNCTION__, array());
|
878 |
|
|
|
879 |
|
|
$options += array(
|
880 |
|
|
'restricted' => FALSE,
|
881 |
|
|
'depth' => 4,
|
882 |
|
|
'data' => array(),
|
883 |
|
|
'values' => FALSE,
|
884 |
|
|
'flat' => FALSE,
|
885 |
|
|
);
|
886 |
|
|
|
887 |
|
|
// Do not allow past the maximum token information depth.
|
888 |
|
|
$options['depth'] = min($options['depth'], TOKEN_MAX_DEPTH);
|
889 |
|
|
|
890 |
|
|
// If $token_type is an entity, make sure we are using the actual token type.
|
891 |
|
|
if ($entity_token_type = token_get_entity_mapping('entity', $token_type)) {
|
892 |
|
|
$token_type = $entity_token_type;
|
893 |
|
|
}
|
894 |
|
|
|
895 |
|
|
$tree_cid = "tree:{$token_type}:{$language->language}:{$options['depth']}";
|
896 |
|
|
|
897 |
|
|
// If we do not have this base tree in the static cache, check {cache_token}
|
898 |
|
|
// otherwise generate and store it in the cache.
|
899 |
|
|
if (!isset($trees[$tree_cid])) {
|
900 |
|
|
if ($cache = cache_get($tree_cid, 'cache_token')) {
|
901 |
|
|
$trees[$tree_cid] = $cache->data;
|
902 |
|
|
}
|
903 |
|
|
else {
|
904 |
|
|
$options['parents'] = array();
|
905 |
|
|
$trees[$tree_cid] = _token_build_tree($token_type, $options);
|
906 |
|
|
cache_set($tree_cid, $trees[$tree_cid], 'cache_token');
|
907 |
|
|
}
|
908 |
|
|
}
|
909 |
|
|
|
910 |
|
|
$tree = $trees[$tree_cid];
|
911 |
|
|
|
912 |
|
|
// If the user has requested a flat tree, convert it.
|
913 |
|
|
if (!empty($options['flat'])) {
|
914 |
|
|
$tree = token_flatten_tree($tree);
|
915 |
|
|
}
|
916 |
|
|
|
917 |
|
|
// Fill in token values.
|
918 |
|
|
if (!empty($options['values'])) {
|
919 |
|
|
$token_values = array();
|
920 |
|
|
foreach ($tree as $token => $token_info) {
|
921 |
|
|
if (!empty($token_info['dynamic']) || !empty($token_info['restricted'])) {
|
922 |
|
|
continue;
|
923 |
|
|
}
|
924 |
|
|
elseif (!isset($token_info['value'])) {
|
925 |
|
|
$token_values[$token_info['token']] = $token;
|
926 |
|
|
}
|
927 |
|
|
}
|
928 |
|
|
if (!empty($token_values)) {
|
929 |
|
|
$token_values = token_generate($token_type, $token_values, $options['data']);
|
930 |
|
|
foreach ($token_values as $token => $replacement) {
|
931 |
|
|
$tree[$token]['value'] = $replacement;
|
932 |
|
|
}
|
933 |
|
|
}
|
934 |
|
|
}
|
935 |
|
|
|
936 |
|
|
return $tree;
|
937 |
|
|
}
|
938 |
|
|
|
939 |
|
|
/**
|
940 |
|
|
* Flatten a token tree.
|
941 |
|
|
*/
|
942 |
|
|
function token_flatten_tree($tree) {
|
943 |
|
|
$result = array();
|
944 |
|
|
foreach ($tree as $token => $token_info) {
|
945 |
|
|
$result[$token] = $token_info;
|
946 |
|
|
if (isset($token_info['children']) && is_array($token_info['children'])) {
|
947 |
|
|
$result += token_flatten_tree($token_info['children']);
|
948 |
|
|
// unset($result[$token]['children']);
|
949 |
|
|
}
|
950 |
|
|
}
|
951 |
|
|
return $result;
|
952 |
|
|
}
|
953 |
|
|
|
954 |
|
|
/**
|
955 |
|
|
* Generate a token tree.
|
956 |
|
|
*/
|
957 |
|
|
function _token_build_tree($token_type, array $options) {
|
958 |
|
|
$options += array(
|
959 |
|
|
'parents' => array(),
|
960 |
|
|
);
|
961 |
|
|
|
962 |
|
|
$info = token_get_info();
|
963 |
|
|
if ($options['depth'] <= 0 || !isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
|
964 |
|
|
return array();
|
965 |
|
|
}
|
966 |
|
|
|
967 |
|
|
$tree = array();
|
968 |
|
|
foreach ($info['tokens'][$token_type] as $token => $token_info) {
|
969 |
|
|
// Build the raw token string.
|
970 |
|
|
$token_parents = $options['parents'];
|
971 |
|
|
if (empty($token_parents)) {
|
972 |
|
|
// If the parents array is currently empty, assume the token type is its
|
973 |
|
|
// parent.
|
974 |
|
|
$token_parents[] = $token_type;
|
975 |
|
|
}
|
976 |
|
|
elseif (in_array($token, array_slice($token_parents, 1))) {
|
977 |
|
|
// Prevent duplicate recursive tokens. For example, this will prevent
|
978 |
|
|
// the tree from generating the following tokens or deeper:
|
979 |
|
|
// [comment:parent:parent]
|
980 |
|
|
// [comment:parent:root:parent]
|
981 |
|
|
continue;
|
982 |
|
|
}
|
983 |
|
|
|
984 |
|
|
$token_parents[] = $token;
|
985 |
|
|
if (!empty($token_info['dynamic'])) {
|
986 |
|
|
$token_parents[] = '?';
|
987 |
|
|
}
|
988 |
|
|
$raw_token = '[' . implode(':', $token_parents) . ']';
|
989 |
|
|
$tree[$raw_token] = $token_info;
|
990 |
|
|
$tree[$raw_token]['raw token'] = $raw_token;
|
991 |
|
|
|
992 |
|
|
// Add the token's real name (leave out the base token type).
|
993 |
|
|
$tree[$raw_token]['token'] = implode(':', array_slice($token_parents, 1));
|
994 |
|
|
|
995 |
|
|
// Add the token's parent as its raw token value.
|
996 |
|
|
if (!empty($options['parents'])) {
|
997 |
|
|
$tree[$raw_token]['parent'] = '[' . implode(':', $options['parents']) . ']';
|
998 |
|
|
}
|
999 |
|
|
|
1000 |
|
|
// Fetch the child tokens.
|
1001 |
|
|
if (!empty($token_info['type'])) {
|
1002 |
|
|
$child_options = $options;
|
1003 |
|
|
$child_options['depth']--;
|
1004 |
|
|
$child_options['parents'] = $token_parents;
|
1005 |
|
|
$tree[$raw_token]['children'] = _token_build_tree($token_info['type'], $child_options);
|
1006 |
|
|
}
|
1007 |
|
|
}
|
1008 |
|
|
|
1009 |
|
|
return $tree;
|
1010 |
|
|
}
|
1011 |
|
|
|
1012 |
|
|
/**
|
1013 |
|
|
* Get a translated menu link by its mlid, without access checking.
|
1014 |
|
|
*
|
1015 |
|
|
* This function is a copy of menu_link_load() but with its own cache and a
|
1016 |
|
|
* simpler query to load the link. This also skips normal menu link access
|
1017 |
|
|
* checking by using _token_menu_link_translate().
|
1018 |
|
|
*
|
1019 |
|
|
* @param $mlid
|
1020 |
|
|
* The mlid of the menu item.
|
1021 |
|
|
*
|
1022 |
|
|
* @return
|
1023 |
|
|
* A menu link translated for rendering.
|
1024 |
|
|
*
|
1025 |
|
|
* @see menu_link_load()
|
1026 |
|
|
* @see _token_menu_link_translate()
|
1027 |
|
|
*/
|
1028 |
|
|
function token_menu_link_load($mlid) {
|
1029 |
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
1030 |
|
|
|
1031 |
|
|
if (!is_numeric($mlid)) {
|
1032 |
|
|
return FALSE;
|
1033 |
|
|
}
|
1034 |
|
|
|
1035 |
|
|
if (!isset($cache[$mlid])) {
|
1036 |
|
|
$item = db_query("SELECT * FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = :mlid", array(':mlid' => $mlid))->fetchAssoc();
|
1037 |
|
|
if (!empty($item)) {
|
1038 |
|
|
_token_menu_link_translate($item);
|
1039 |
|
|
}
|
1040 |
|
|
$cache[$mlid] = $item;
|
1041 |
|
|
}
|
1042 |
|
|
|
1043 |
|
|
return $cache[$mlid];
|
1044 |
|
|
}
|
1045 |
|
|
|
1046 |
|
|
/**
|
1047 |
|
|
* Get a translated book menu link by its mlid, without access checking.
|
1048 |
|
|
*
|
1049 |
|
|
* This function is a copy of book_link_load() but with its own cache and a
|
1050 |
|
|
* simpler query to load the link. This also skips normal menu link access
|
1051 |
|
|
* checking by using _token_menu_link_translate().
|
1052 |
|
|
*
|
1053 |
|
|
* @param $mlid
|
1054 |
|
|
* The mlid of the book menu item.
|
1055 |
|
|
*
|
1056 |
|
|
* @return
|
1057 |
|
|
* A book menu link translated for rendering.
|
1058 |
|
|
*
|
1059 |
|
|
* @see book_link_load()
|
1060 |
|
|
* @see _token_menu_link_translate()
|
1061 |
|
|
*/
|
1062 |
|
|
function token_book_link_load($mlid) {
|
1063 |
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
1064 |
|
|
|
1065 |
|
|
if (!is_numeric($mlid)) {
|
1066 |
|
|
return FALSE;
|
1067 |
|
|
}
|
1068 |
|
|
|
1069 |
|
|
if (!isset($cache[$mlid])) {
|
1070 |
|
|
$item = db_query("SELECT * FROM {menu_links} ml INNER JOIN {book} b ON b.mlid = ml.mlid LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = :mlid", array(':mlid' => $mlid))->fetchAssoc();
|
1071 |
|
|
if (!empty($item)) {
|
1072 |
|
|
_token_menu_link_translate($item);
|
1073 |
|
|
}
|
1074 |
|
|
$cache[$mlid] = $item;
|
1075 |
|
|
}
|
1076 |
|
|
|
1077 |
|
|
return $cache[$mlid];
|
1078 |
|
|
}
|
1079 |
|
|
|
1080 |
|
|
function _token_menu_link_translate(&$item) {
|
1081 |
|
|
$map = array();
|
1082 |
|
|
|
1083 |
|
|
if (!is_array($item['options'])) {
|
1084 |
|
|
$item['options'] = unserialize($item['options']);
|
1085 |
|
|
}
|
1086 |
|
|
|
1087 |
|
|
if ($item['external']) {
|
1088 |
|
|
$item['access'] = 1;
|
1089 |
|
|
$item['href'] = $item['link_path'];
|
1090 |
|
|
$item['title'] = $item['link_title'];
|
1091 |
|
|
$item['localized_options'] = $item['options'];
|
1092 |
|
|
}
|
1093 |
|
|
else {
|
1094 |
|
|
// Complete the path of the menu link with elements from the current path,
|
1095 |
|
|
// if it contains dynamic placeholders (%).
|
1096 |
|
|
$map = explode('/', $item['link_path']);
|
1097 |
|
|
if (strpos($item['link_path'], '%') !== FALSE) {
|
1098 |
|
|
// Invoke registered to_arg callbacks.
|
1099 |
|
|
if (!empty($item['to_arg_functions'])) {
|
1100 |
|
|
_menu_link_map_translate($map, $item['to_arg_functions']);
|
1101 |
|
|
}
|
1102 |
|
|
}
|
1103 |
|
|
$item['href'] = implode('/', $map);
|
1104 |
|
|
|
1105 |
|
|
// Skip links containing untranslated arguments.
|
1106 |
|
|
if (strpos($item['href'], '%') !== FALSE) {
|
1107 |
|
|
$item['access'] = FALSE;
|
1108 |
|
|
return FALSE;
|
1109 |
|
|
}
|
1110 |
|
|
|
1111 |
|
|
$item['access'] = TRUE;
|
1112 |
|
|
_menu_item_localize($item, $map, TRUE);
|
1113 |
|
|
}
|
1114 |
|
|
|
1115 |
|
|
// Allow other customizations - e.g. adding a page-specific query string to the
|
1116 |
|
|
// options array. For performance reasons we only invoke this hook if the link
|
1117 |
|
|
// has the 'alter' flag set in the options array.
|
1118 |
|
|
if (!empty($item['options']['alter'])) {
|
1119 |
|
|
drupal_alter('translated_menu_link', $item, $map);
|
1120 |
|
|
}
|
1121 |
|
|
|
1122 |
|
|
return $map;
|
1123 |
|
|
}
|
1124 |
|
|
|
1125 |
|
|
/**
|
1126 |
|
|
* Prepare a string for use as a valid token name.
|
1127 |
|
|
*
|
1128 |
|
|
* @param $name
|
1129 |
|
|
* The token name to clean.
|
1130 |
|
|
* @return
|
1131 |
|
|
* The cleaned token name.
|
1132 |
|
|
*/
|
1133 |
|
|
function token_clean_token_name($name) {
|
1134 |
|
|
static $names = array();
|
1135 |
|
|
|
1136 |
|
|
if (!isset($names[$name])) {
|
1137 |
|
|
$cleaned_name = strtr($name, array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
|
1138 |
|
|
$cleaned_name = preg_replace('/[^\w\-]/i', '', $cleaned_name);
|
1139 |
|
|
$cleaned_name = trim($cleaned_name, '-');
|
1140 |
|
|
$names[$name] = $cleaned_name;
|
1141 |
|
|
}
|
1142 |
|
|
|
1143 |
|
|
return $names[$name];
|
1144 |
|
|
}
|
1145 |
|
|
|
1146 |
|
|
/**
|
1147 |
|
|
* Do not use this function yet. Its API has not been finalized.
|
1148 |
|
|
*/
|
1149 |
|
|
function token_render_array(array $array, array $options = array()) {
|
1150 |
|
|
$rendered = array();
|
1151 |
|
|
foreach (element_children($array) as $key) {
|
1152 |
|
|
$value = $array[$key];
|
1153 |
|
|
$rendered[] = is_array($value) ? render($value) : (string) $value;
|
1154 |
|
|
}
|
1155 |
|
|
if (!empty($options['sanitize'])) {
|
1156 |
|
|
$rendered = array_map('check_plain', $rendered);
|
1157 |
|
|
}
|
1158 |
|
|
$join = isset($options['join']) ? $options['join'] : ', ';
|
1159 |
|
|
return implode($join, $rendered);
|
1160 |
|
|
}
|
1161 |
|
|
|
1162 |
|
|
/**
|
1163 |
|
|
* Do not use this function yet. Its API has not been finalized.
|
1164 |
|
|
*/
|
1165 |
|
|
function token_render_array_value($value, array $options = array()) {
|
1166 |
|
|
$rendered = is_array($value) ? render($value) : (string) $value;
|
1167 |
|
|
if (!empty($options['sanitize'])) {
|
1168 |
|
|
$rendered = check_plain($rendered);
|
1169 |
|
|
}
|
1170 |
|
|
return $rendered;
|
1171 |
|
|
}
|
1172 |
|
|
|
1173 |
|
|
/**
|
1174 |
|
|
* Copy of drupal_render_cache_get() that does not care about request method.
|
1175 |
|
|
*/
|
1176 |
|
|
function token_render_cache_get($elements) {
|
1177 |
|
|
if (!$cid = drupal_render_cid_create($elements)) {
|
1178 |
|
|
return FALSE;
|
1179 |
|
|
}
|
1180 |
|
|
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
|
1181 |
|
|
|
1182 |
|
|
if (!empty($cid) && $cache = cache_get($cid, $bin)) {
|
1183 |
|
|
// Add additional libraries, JavaScript, CSS and other data attached
|
1184 |
|
|
// to this element.
|
1185 |
|
|
if (isset($cache->data['#attached'])) {
|
1186 |
|
|
drupal_process_attached($cache->data);
|
1187 |
|
|
}
|
1188 |
|
|
// Return the rendered output.
|
1189 |
|
|
return $cache->data['#markup'];
|
1190 |
|
|
}
|
1191 |
|
|
return FALSE;
|
1192 |
|
|
}
|
1193 |
|
|
|
1194 |
|
|
/**
|
1195 |
|
|
* Coyp of drupal_render_cache_set() that does not care about request method.
|
1196 |
|
|
*/
|
1197 |
|
|
function token_render_cache_set(&$markup, $elements) {
|
1198 |
|
|
// This should only run of drupal_render_cache_set() did not.
|
1199 |
|
|
if (in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'))) {
|
1200 |
|
|
return FALSE;
|
1201 |
|
|
}
|
1202 |
|
|
|
1203 |
|
|
$original_method = $_SERVER['REQUEST_METHOD'];
|
1204 |
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
1205 |
|
|
drupal_render_cache_set($markup, $elements);
|
1206 |
|
|
$_SERVER['REQUEST_METHOD'] = $original_method;
|
1207 |
|
|
}
|
1208 |
|
|
|
1209 |
|
|
function token_menu_link_load_all_parents($mlid) {
|
1210 |
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
1211 |
|
|
|
1212 |
|
|
if (!is_numeric($mlid)) {
|
1213 |
|
|
return array();
|
1214 |
|
|
}
|
1215 |
|
|
|
1216 |
|
|
if (!isset($cache[$mlid])) {
|
1217 |
|
|
$cache[$mlid] = array();
|
1218 |
|
|
$plid = db_query("SELECT plid FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
|
1219 |
|
|
while ($plid && $parent = token_menu_link_load($plid)) {
|
1220 |
|
|
$cache[$mlid] = array($plid => $parent['title']) + $cache[$mlid];
|
1221 |
|
|
$plid = $parent['plid'];
|
1222 |
|
|
}
|
1223 |
|
|
}
|
1224 |
|
|
|
1225 |
|
|
return $cache[$mlid];
|
1226 |
|
|
}
|
1227 |
|
|
|
1228 |
|
|
function token_taxonomy_term_load_all_parents($tid) {
|
1229 |
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
1230 |
|
|
|
1231 |
|
|
if (!is_numeric($tid)) {
|
1232 |
|
|
return array();
|
1233 |
|
|
}
|
1234 |
|
|
|
1235 |
|
|
if (!isset($cache[$tid])) {
|
1236 |
|
|
$cache[$tid] = array();
|
1237 |
|
|
$parents = taxonomy_get_parents_all($tid);
|
1238 |
|
|
array_shift($parents); // Remove this term from the array.
|
1239 |
|
|
$parents = array_reverse($parents);
|
1240 |
|
|
foreach ($parents as $term) {
|
1241 |
|
|
$cache[$tid][$term->tid] = entity_label('taxonomy_term', $term);
|
1242 |
|
|
}
|
1243 |
|
|
}
|
1244 |
|
|
|
1245 |
|
|
return $cache[$tid];
|
1246 |
|
|
}
|
1247 |
|
|
|
1248 |
|
|
/**
|
1249 |
|
|
* Load the preferred menu link associated with a node.
|
1250 |
|
|
*
|
1251 |
|
|
* @param $node
|
1252 |
|
|
* A node object.
|
1253 |
|
|
*
|
1254 |
|
|
* @return
|
1255 |
|
|
* A menu link array from token_menu_link_load() or FALSE if a menu link was
|
1256 |
|
|
* not found.
|
1257 |
|
|
*
|
1258 |
|
|
* @see menu_node_prepare()
|
1259 |
|
|
* @see token_menu_link_load()
|
1260 |
|
|
*/
|
1261 |
|
|
function token_node_menu_link_load($node) {
|
1262 |
|
|
$cache = &drupal_static(__FUNCTIon__, array());
|
1263 |
|
|
|
1264 |
|
|
if (!isset($cache[$node->nid])) {
|
1265 |
|
|
$mlid = FALSE;
|
1266 |
|
|
|
1267 |
|
|
// Nodes do not have their menu links loaded via menu_node_load().
|
1268 |
|
|
if (!isset($node->menu)) {
|
1269 |
|
|
// We need to clone the node as menu_node_prepare() may cause data loss.
|
1270 |
|
|
// @see http://drupal.org/node/1317926
|
1271 |
|
|
$menu_node = clone $node;
|
1272 |
|
|
menu_node_prepare($menu_node);
|
1273 |
|
|
$mlid = !empty($menu_node->menu['mlid']) ? $menu_node->menu['mlid'] : FALSE;
|
1274 |
|
|
}
|
1275 |
|
|
else {
|
1276 |
|
|
$mlid = !empty($node->menu['mlid']) ? $node->menu['mlid'] : FALSE;
|
1277 |
|
|
}
|
1278 |
|
|
|
1279 |
|
|
$cache[$node->nid] = $mlid;
|
1280 |
|
|
}
|
1281 |
|
|
|
1282 |
|
|
return $cache[$node->nid] ? token_menu_link_load($cache[$node->nid]) : FALSE;
|
1283 |
|
|
} |