token.module 40 KB

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