token.module 40 KB

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