token.module 39.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
<?php

/**
 * @file
 * Enhances the token API in core: adds a browseable UI, missing tokens, etc.
 */

/**
 * The maximum depth for token tree recursion.
 */
define('TOKEN_MAX_DEPTH', 9);

/**
 * Implements hook_help().
 */
function token_help($path, $arg) {
  if ($path == 'admin/help#token') {
    if (current_path() != 'admin/help/token') {
      // Because system_modules() executes hook_help() for each module to 'test'
      // if they will return anything, but not actually display it, we want to
      // return a TRUE value if this is not actually the help page.
      return TRUE;
    }
    $output = '<dl>';
    $output .= '<dt>' . t('List of the currently available tokens on this site') . '</dt>';
    $output .= '<dd>' . theme('token_tree', array('token_types' => 'all', 'click_insert' => FALSE, 'show_restricted' => TRUE)) . '</dd>';
    $output .= '</dl>';
    return $output;
  }
}

/**
 * Implements hook_system_info_alter().
 *
 * Prevent the token_actions module from being enabled since updates may have
 * left the old module files still in the directory.
 */
function token_system_info_alter(&$info, $file, $type) {
  if ($type == 'module' && $file->name == 'token_actions') {
    $info['hidden'] = TRUE;
  }
}

/**
 * Return an array of the core modules supported by token.module.
 */
function _token_core_supported_modules() {
  return array('book', 'field', 'menu', 'profile');
}

/**
 * Implements hook_menu().
 */
function token_menu() {
  /*$items['token/autocomplete/all/%menu_tail'] = array(
    'page callback' => 'token_autocomplete',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'token.pages.inc',
  );*/
  $items['token/autocomplete/%token_type'] = array(
    'page callback' => 'token_autocomplete_token',
    'page arguments' => array(2),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'token.pages.inc',
  );
  /*$items['token/autocomplete/%token_type/%menu_tail'] = array(
    'page callback' => 'token_autocomplete_token',
    'page arguments' => array(2, 3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'token.pages.inc',
  );*/

  $items['token/tree'] = array(
    'page callback' => 'token_page_output_tree',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'token.pages.inc',
    'theme callback' => 'ajax_base_page_theme',
  );

  // Devel token pages.
  if (module_exists('devel')) {
    $items['node/%node/devel/token'] = array(
      'title' => 'Tokens',
      'page callback' => 'token_devel_token_object',
      'page arguments' => array('node', 1),
      'access arguments' => array('access devel information'),
      'type' => MENU_LOCAL_TASK,
      'file' => 'token.pages.inc',
      'weight' => 5,
    );
    $items['comment/%comment/devel/token'] = array(
      'title' => 'Tokens',
      'page callback' => 'token_devel_token_object',
      'page arguments' => array('comment', 1),
      'access arguments' => array('access devel information'),
      'type' => MENU_LOCAL_TASK,
      'file' => 'token.pages.inc',
      'weight' => 5,
    );
    $items['taxonomy/term/%taxonomy_term/devel/token'] = array(
      'title' => 'Tokens',
      'page callback' => 'token_devel_token_object',
      'page arguments' => array('taxonomy_term', 2),
      'access arguments' => array('access devel information'),
      'type' => MENU_LOCAL_TASK,
      'file' => 'token.pages.inc',
      'weight' => 5,
    );
    $items['user/%user/devel/token'] = array(
      'title' => 'Tokens',
      'page callback' => 'token_devel_token_object',
      'page arguments' => array('user', 1),
      'access arguments' => array('access devel information'),
      'type' => MENU_LOCAL_TASK,
      'file' => 'token.pages.inc',
      'weight' => 5,
    );
  }

  // Admin menu callback to clear token caches.
  $items['token/flush-cache'] = array(
    'page callback' => 'token_flush_cache_callback',
    'access arguments' => array('flush caches'),
    'type' => MENU_CALLBACK,
    'file' => 'token.pages.inc',
  );

  return $items;
}

/**
 * Implements hook_admin_menu_output_alter().
 */
function token_admin_menu_output_alter(&$content) {
  $content['icon']['icon']['flush-cache']['token'] = array(
    '#title' => t('Token registry'),
    '#href' => 'token/flush-cache',
    '#options' => array(
      'query' => drupal_get_destination() + array('token' => drupal_get_token('token/flush-cache')),
    ),
  );
}

function token_type_load($token_type) {
  $info = token_get_info();
  return isset($info['types'][$token_type]) ? $info['types'][$token_type] : FALSE;
}

/**
 * Implements hook_theme().
 */
function token_theme() {
  $info['tree_table'] = array(
    'variables' => array(
      'header' => array(),
      'rows' => array(),
      'attributes' => array(),
      'empty' => '',
      'caption' => '',
    ),
    'file' => 'token.pages.inc',
  );
  $info['token_tree'] = array(
    'variables' => array(
      'token_types' => array(),
      'global_types' => TRUE,
      'click_insert' => TRUE,
      'show_restricted' => FALSE,
      'recursion_limit' => 3,
      'dialog' => FALSE,
    ),
    'file' => 'token.pages.inc',
  );
  $info['token_tree_link'] = array(
    'variables' => array(
      'text' => NULL,
      'options' => array(),
      'dialog' => TRUE,
    ),
    'file' => 'token.pages.inc',
  );

  return $info;
}

/**
 * Implements hook_library().
 */
function token_library() {
  // jQuery treeTable plugin.
  $libraries['treeTable'] = array(
    'title' => 'jQuery treeTable',
    'website' => 'http://plugins.jquery.com/project/treetable',
    'version' => '2.3.0',
    'js' => array(
      drupal_get_path('module', 'token') . '/jquery.treeTable.js' => array(),
    ),
    'css' => array(
      drupal_get_path('module', 'token') . '/jquery.treeTable.css' => array(),
    ),
  );

  $libraries['dialog'] = array(
    'title' => 'Token dialog',
    'version' => '1.0',
    'js' => array(
      drupal_get_path('module', 'token') . '/token.js' => array(),
    ),
    'dependencies' => array(
      array('system', 'ui.dialog'),
    ),
  );

  return $libraries;
}

/**
 * Implements hook_form_alter().
 *
 * Adds a submit handler to forms which could affect the tokens available on
 * the site.
 */
function token_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    // Profile field forms.
    case 'profile_field_form':
    case 'profile_field_delete':
    // User picture form.
    case 'user_admin_settings':
    // System date type form.
    // @todo Remove when http://drupal.org/node/1173706 is fixed.
    case 'system_add_date_format_type_form':
    case 'system_delete_date_format_type_form':
      $form += array('#submit' => array());
      array_unshift($form['#submit'], 'token_clear_cache');
      break;
  }
}

/**
 * Implements hook_block_view_alter().
 */
function token_block_view_alter(&$data, $block) {
  if (!empty($block->title) && $block->title != '<none>') {
    // Perform unsanitized token replacement since _block_render_blocks() will
    // call check_plain() on $block->title.
    $block->title = token_replace($block->title, array(), array('sanitize' => FALSE));
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function token_form_block_add_block_form_alter(&$form, $form_state) {
  token_form_block_admin_configure_alter($form, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function token_form_block_admin_configure_alter(&$form, $form_state) {
  $form['settings']['title']['#description'] .= ' ' . t('This field supports tokens.');
  // @todo Figure out why this token validation does not seem to be working here.
  $form['settings']['title']['#element_validate'][] = 'token_element_validate';
  $form['settings']['title'] += array('#token_types' => array());
}

/**
 * Implements hook_widget_form_alter().
 */
function token_field_widget_form_alter(&$element, &$form_state, $context) {
  if (!empty($element['#description']) && is_string($element['#description'])) {
    $element['#description'] = filter_xss_admin(token_replace($element['#description']));
  }
}

/**
 * Implements hook_field_info_alter().
 */
function token_field_info_alter(&$info) {
  $defaults = array(
    'taxonomy_term_reference' => 'taxonomy_term_reference_plain',
    'number_integer' => 'number_unformatted',
    'number_decimal' => 'number_unformatted',
    'number_float' => 'number_unformatted',
    'file' => 'file_url_plain',
    'image' => 'file_url_plain',
    'text' => 'text_default',
    'text_long' => 'text_default',
    'text_with_summary' => 'text_default',
    'list_integer' => 'list_default',
    'list_float' => 'list_default',
    'list_text' => 'list_default',
    'list_boolean' => 'list_default',
  );
  foreach ($defaults as $field_type => $default_token_formatter) {
    if (isset($info[$field_type])) {
      $info[$field_type] += array('default_token_formatter' => $default_token_formatter);
    }
  }
}

/**
 * Implements hook_field_display_alter().
 */
function token_field_display_alter(&$display, $context) {
  if ($context['view_mode'] == 'token') {
    $view_mode_settings = field_view_mode_settings($context['instance']['entity_type'], $context['instance']['bundle']);
    // If the token view mode fell back to the 'default' view mode, then
    // use the default token formatter.
    if (empty($view_mode_settings[$context['view_mode']]['custom_settings'])) {
      $field_type_info = field_info_field_types($context['field']['type']);

      // If the field has specified a specific formatter to be used by default
      // with tokens, use that, otherwise use the default formatter.
      $formatter = !empty($field_type_info['default_token_formatter']) ? $field_type_info['default_token_formatter'] : $field_type_info['default_formatter'];

      // Now that we have a formatter, fill in all the settings.
      $display['type'] = $formatter;
      $formatter_info = field_info_formatter_types($formatter);
      $display['settings'] = isset($formatter_info['settings']) ? $formatter_info['settings'] : array();
      $display['settings']['label'] = 'hidden';
      $display['module'] = $formatter_info['module'];
    }
  }
}

/**
 * Implements hook_field_create_instance().
 */
function token_field_create_instance($instance) {
  token_clear_cache();
}

/**
 * Implements hook_field_update_instance().
 */
function token_field_update_instance($instance) {
  token_clear_cache();
}

/**
 * Implements hook_field_delete_instance().
 */
function token_field_delete_instance($instance) {
  token_clear_cache();
}

/**
 * Clear token caches and static variables.
 */
function token_clear_cache() {
  if (db_table_exists('cache_token')) {
    cache_clear_all('*', 'cache_token', TRUE);
  }
  drupal_static_reset('token_get_info');
  drupal_static_reset('token_get_global_token_types');
  drupal_static_reset('token_get_entity_mapping');
  drupal_static_reset('token_build_tree');
  drupal_static_reset('_token_profile_fields');
  drupal_static_reset('token_menu_link_load');
  drupal_static_reset('token_book_link_load');
  drupal_static_reset('token_node_menu_link_load');
  drupal_static_reset('_token_field_info');
}

/**
 * Return an array of entity type to token type mappings.
 *
 * Why do we need this? Because when the token API was moved to core we did not
 * re-use the entity type as the base name for taxonomy terms and vocabulary
 * tokens.
 *
 * @see token_entity_info_alter()
 * @see http://drupal.org/node/737726
 */
function token_get_entity_mapping($value_type = 'token', $value = NULL, $fallback = FALSE) {
  $mapping = &drupal_static(__FUNCTION__, array());

  if (empty($mapping)) {
    foreach (entity_get_info() as $entity_type => $info) {
      $mapping[$entity_type] = !empty($info['token type']) ? $info['token type'] : $entity_type;
    }
    // Allow modules to alter the mapping array.
    drupal_alter('token_entity_mapping', $mapping);
  }

  if (!isset($value)) {
    return $value_type == 'token' ? array_flip($mapping) : $mapping;
  }
  elseif ($value_type == 'token') {
    $return = array_search($value, $mapping);
    return $return !== FALSE ? $return : ($fallback ? $value : FALSE);
  }
  elseif ($value_type == 'entity') {
    return isset($mapping[$value]) ? $mapping[$value] : ($fallback ? $value : FALSE);
  }
}

/**
 * Implements hook_entity_info_alter().
 *
 * Because some token types to do not match their entity type names, we have to
 * map them to the proper type. This is purely for other modules' benefit.
 *
 * @see token_get_entity_mapping()
 * @see http://drupal.org/node/737726
 */
function token_entity_info_alter(&$info) {
  foreach (array_keys($info) as $entity_type) {
    // Add a token view mode if it does not already exist.
    if (!empty($info[$entity_type]['view modes']) && !isset($info[$entity_type]['view modes']['token'])) {
      $info[$entity_type]['view modes']['token'] = array(
        'label' => t('Tokens'),
        'custom settings' => FALSE,
      );
    }

    if (!empty($info[$entity_type]['token type'])) {
      // If the entity's token type is already defined, great!
      continue;
    }

    // Fill in default token types for entities.
    switch ($entity_type) {
      case 'taxonomy_term':
      case 'taxonomy_vocabulary':
        // Stupid taxonomy token types...
        $info[$entity_type]['token type'] = str_replace('taxonomy_', '', $entity_type);
        break;
      default:
        // By default the token type is the same as the entity type.
        $info[$entity_type]['token type'] = $entity_type;
        break;
    }
  }
}

/**
 * Implements hook_module_implements_alter().
 *
 * Adds missing token support for core modules.
 */
function token_module_implements_alter(&$implementations, $hook) {
  module_load_include('inc', 'token', 'token.tokens');

  if ($hook == 'tokens' || $hook == 'token_info' || $hook == 'token_info_alter' || $hook == 'tokens_alter') {
    foreach (_token_core_supported_modules() as $module) {
      if (module_exists($module) && function_exists($module . '_' . $hook)) {
        $implementations[$module] = FALSE;
      }
    }
    // Move token.module to get included first since it is responsible for
    // other modules.
    unset($implementations['token']);
    $implementations = array_merge(array('token' => 'tokens'), $implementations);
  }
}

/**
 * Implements hook_flush_caches().
 */
function token_flush_caches() {
  if (db_table_exists('cache_token')) {
    return array('cache_token');
  }
}

/**
 * Retrieve, sort, store token info from the cache.
 *
 * @param $token_type
 *   The optional token type that if specified will return
 *   $info['types'][$token_type].
 * @param $token
 *   The optional token name if specified will return
 *   $info['tokens'][$token_type][$token].
 *
 * @return
 *   An array of all token information from hook_token_info(), or the array
 *   of a token type or specific token.
 *
 * @see hook_token_info()
 * @see hook_token_info_alter()
 */
function token_get_info($token_type = NULL, $token = NULL) {
  global $language;

  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['token_info'] = &drupal_static(__FUNCTION__);
  }
  $token_info = &$drupal_static_fast['token_info'];

  if (empty($token_info)) {
    $cid = "info:{$language->language}";

    if ($cache = cache_get($cid, 'cache_token')) {
      $token_info = $cache->data;
    }
    else {
      $token_info = token_info();

      foreach (array_keys($token_info['types']) as $type_key) {
        if (isset($token_info['types'][$type_key]['type'])) {
          $base_type = $token_info['types'][$type_key]['type'];
          // If this token type extends another token type, then merge in
          // the base token type's tokens.
          if (isset($token_info['tokens'][$base_type])) {
            $token_info['tokens'] += array($type_key => array());
            $token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
          }
        }
        else {
          // Add a 'type' value to each token type so we can properly use
          // token_type_load().
          $token_info['types'][$type_key]['type'] = $type_key;
        }
      }

      // Pre-sort tokens.
      uasort($token_info['types'], 'token_asort_tokens');
      foreach (array_keys($token_info['tokens']) as $type) {
        uasort($token_info['tokens'][$type], 'token_asort_tokens');
      }

      // Store info in cache for future use.
      cache_set($cid, $token_info, 'cache_token');
    }
  }

  if (isset($token_type) && isset($token)) {
    return isset($token_info['tokens'][$token_type][$token]) ? $token_info['tokens'][$token_type][$token] : NULL;
  }
  elseif (isset($token_type)) {
    return isset($token_info['types'][$token_type]) ? $token_info['types'][$token_type] : NULL;
  }
  else {
    return $token_info;
  }
}

/**
 * Return the module responsible for a token.
 *
 * @param string $type
 *   The token type.
 * @param string $name
 *   The token name.
 *
 * @return mixed
 *   The value of $info['tokens'][$type][$name]['module'] from token_get_info(),
 *   or NULL if the value does not exist.
 */
function _token_module($type, $name) {
  $token_info = token_get_info($type, $name);
  return isset($token_info['module']) ? $token_info['module'] : NULL;
}

/**
 * uasort() callback to sort tokens by the 'name' property.
 */
function token_asort_tokens($token_a, $token_b) {
  return strnatcmp($token_a['name'], $token_b['name']);
}

/**
 * Get a list of token types that can be used without any context (global).
 *
 * @return
 *   An array of global token types.
 */
function token_get_global_token_types() {
  $global_types = &drupal_static(__FUNCTION__, array());

  if (empty($global_types)) {
    $token_info = token_get_info();
    foreach ($token_info['types'] as $type => $type_info) {
      // If the token types has not specified that 'needs-data' => TRUE, then
      // it is a global token type that will always be replaced in any context.
      if (empty($type_info['needs-data'])) {
        $global_types[] = $type;
      }
    }
  }

  return $global_types;
}

/**
 * Validate an tokens in raw text based on possible contexts.
 *
 * @param $value
 *   A string with the raw text containing the raw tokens, or an array of
 *   tokens from token_scan().
 * @param $tokens
 *   An array of token types that will be used when token replacement is
 *   performed.
 * @return
 *   An array with the invalid tokens in their original raw forms.
 */
function token_get_invalid_tokens_by_context($value, $valid_types = array()) {
  if (in_array('all', $valid_types)) {
    $info = token_get_info();
    $valid_types = array_keys($info['types']);
  }
  else {
    // Add the token types that are always valid in global context.
    $valid_types = array_merge($valid_types, token_get_global_token_types());
  }

  $invalid_tokens = array();
  $value_tokens = is_string($value) ? token_scan($value) : $value;

  foreach ($value_tokens as $type => $tokens) {
    if (!in_array($type, $valid_types)) {
      // If the token type is not a valid context, its tokens are invalid.
      $invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
    }
    else {
      // Check each individual token for validity.
      $invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($type, $tokens));
    }
  }

  array_unique($invalid_tokens);
  return $invalid_tokens;
}

/**
 * Validate an array of tokens based on their token type.
 *
 * @param $type
 *   The type of tokens to validate (e.g. 'node', etc.)
 * @param $tokens
 *   A keyed array of tokens, and their original raw form in the source text.
 * @return
 *   An array with the invalid tokens in their original raw forms.
 */
function token_get_invalid_tokens($type, $tokens) {
  $token_info = token_get_info();
  $invalid_tokens = array();

  foreach ($tokens as $token => $full_token) {
    // Split token up if it has chains.
    $parts = explode(':', $token, 2);

    if (!isset($token_info['tokens'][$type][$parts[0]])) {
      // This is an invalid token (not defined).
      $invalid_tokens[] = $full_token;
    }
    elseif (count($parts) == 2) {
      $sub_token_info = $token_info['tokens'][$type][$parts[0]];
      if (!empty($sub_token_info['dynamic'])) {
        // If this token has been flagged as a dynamic token, skip it.
        continue;
      }
      elseif (empty($sub_token_info['type'])) {
        // If the token has chains, but does not support it, it is invalid.
        $invalid_tokens[] = $full_token;
      }
      else {
        // Resursively check the chained tokens.
        $sub_tokens = token_find_with_prefix(array($token => $full_token), $parts[0]);
        $invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($sub_token_info['type'], $sub_tokens));
      }
    }
  }

  return $invalid_tokens;
}

/**
 * Validate a form element that should have tokens in it.
 *
 * Form elements that want to add this validation should have the #token_types
 * parameter defined.
 *
 * For example:
 * @code
 * $form['my_node_text_element'] = array(
 *   '#type' => 'textfield',
 *   '#title' => t('Some text to token-ize that has a node context.'),
 *   '#default_value' => 'The title of this node is [node:title].',
 *   '#element_validate' => array('token_element_validate'),
 *   '#token_types' => array('node'),
 *   '#min_tokens' => 1,
 *   '#max_tokens' => 10,
 * );
 * @endcode
 */
function token_element_validate(&$element, &$form_state) {
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];

  if (!drupal_strlen($value)) {
    // Empty value needs no further validation since the element should depend
    // on using the '#required' FAPI property.
    return $element;
  }

  $tokens = token_scan($value);
  $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
  // @todo Find old Drupal 6 style tokens and add them to invalid tokens.

  // Validate if an element must have a minimum number of tokens.
  if (isset($element['#min_tokens']) && count($tokens) < $element['#min_tokens']) {
    // @todo Change this error message to include the minimum number.
    $error = format_plural($element['#min_tokens'], 'The %element-title cannot contain fewer than one token.', 'The %element-title must contain at least @count tokens.', array('%element-title' => $title));
    form_error($element, $error);
  }

  // Validate if an element must have a maximum number of tokens.
  if (isset($element['#max_tokens']) && count($tokens) > $element['#max_tokens']) {
    // @todo Change this error message to include the maximum number.
    $error = format_plural($element['#max_tokens'], 'The %element-title must contain as most one token.', 'The %element-title must contain at most @count tokens.', array('%element-title' => $title));
    form_error($element, $error);
  }

  // Check if the field defines specific token types.
  if (isset($element['#token_types'])) {
    $invalid_tokens = token_get_invalid_tokens_by_context($tokens, $element['#token_types']);
    if ($invalid_tokens) {
      form_error($element, t('The %element-title is using the following invalid tokens: @invalid-tokens.', array('%element-title' => $title, '@invalid-tokens' => implode(', ', $invalid_tokens))));
    }
  }

  return $element;
}

/**
 * Deprecated. Use token_element_validate() instead.
 */
function token_element_validate_token_context(&$element, &$form_state) {
  return token_element_validate($element, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function token_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (!isset($form['instance']) || !empty($form['#field']['locked'])) {
    return;
  }

  if (($form['#field']['type'] == 'file' || $form['#field']['type'] == 'image') && isset($form['instance']['settings']['file_directory']) && !module_exists('filefield_paths')) {
    // GAH! We can only support global tokens in the upload file directory path.
    $form['instance']['settings']['file_directory']['#element_validate'][] = 'token_element_validate';
    $form['instance']['settings']['file_directory'] += array('#token_types' => array());
    $form['instance']['settings']['file_directory']['#description'] .= ' ' . t('This field supports tokens.');
  }

  // Note that the description is tokenized via token_field_widget_form_alter().
  $form['instance']['description']['#description'] .= '<br />' . t('This field supports tokens.');
  $form['instance']['description']['#element_validate'][] = 'token_element_validate';
  $form['instance']['description'] += array('#token_types' => array());

  $form['instance']['settings']['token_tree'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array(),
    '#dialog' => TRUE,
    '#weight' => $form['instance']['description']['#weight'] + 0.5,
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the configure action form to add token context validation and
 * adds the token tree for a better token UI and selection.
 */
function token_form_system_actions_configure_alter(&$form, $form_state) {
  $action = actions_function_lookup($form['actions_action']['#value']);

  switch ($action) {
    case 'system_message_action':
    case 'system_send_email_action':
    case 'system_goto_action':
      $form['token_tree'] = array(
        '#theme' => 'token_tree',
        '#token_types' => 'all',
        '#dialog' => TRUE,
        '#weight' => 100,
      );
      // @todo Add token validation to the action fields that can use tokens.
      break;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the user e-mail fields to add token context validation and
 * adds the token tree for a better token UI and selection.
 */
function token_form_user_admin_settings_alter(&$form, &$form_state) {
  $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].');

  foreach (element_children($form) as $key) {
    $element = &$form[$key];

    // Remove the crummy default token help text.
    if (!empty($element['#description'])) {
      $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']));
    }

    switch ($key) {
      case 'email_admin_created':
      case 'email_pending_approval':
      case 'email_no_approval_required':
      case 'email_password_reset':
      case 'email_cancel_confirm':
        // Do nothing, but allow execution to continue.
        break;
      case 'email_activated':
      case 'email_blocked':
      case 'email_canceled':
        // These fieldsets have their e-mail elements inside a 'settings'
        // sub-element, so switch to that element instead.
        $element = &$form[$key]['settings'];
        break;
      default:
        continue 2;
    }

    foreach (element_children($element) as $sub_key) {
      if (!isset($element[$sub_key]['#type'])) {
        continue;
      }
      elseif ($element[$sub_key]['#type'] == 'textfield' && substr($sub_key, -8) === '_subject') {
        // Add validation to subject textfields.
        $element[$sub_key]['#element_validate'][] = 'token_element_validate';
        $element[$sub_key] += array('#token_types' => array('user'));
      }
      elseif ($element[$sub_key]['#type'] == 'textarea' && substr($sub_key, -5) === '_body') {
        // Add validation to body textareas.
        $element[$sub_key]['#element_validate'][] = 'token_element_validate';
        $element[$sub_key] += array('#token_types' => array('user'));
      }
    }
  }

  // Add the token tree UI.
  $form['email']['token_tree'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array('user'),
    '#show_restricted' => TRUE,
    '#dialog' => TRUE,
    '#weight' => 90,
  );
}

/**
 * Build a tree array of tokens used for themeing or information.
 *
 * @param $token_type
 *   The token type.
 * @param $flat_tree
 *   A boolean if TRUE will only make a flat array of tokens, otherwise
 *   child tokens will be inside the 'children' parameter of a token.
 * @param $show_restricted
 *   A boolean if TRUE will show restricted tokens. Otherwise they will be
 *   hidden. Default is FALSE.
 * @param $recursion_limit
 *   An integer with the maximum number of token levels to recurse.
 * @param $parents
 *   An optional array with the current parents of the tokens.
 */
function token_build_tree($token_type, array $options = array()) {
  global $language;

  // Static cache of already built token trees.
  $trees = &drupal_static(__FUNCTION__, array());

  $options += array(
    'restricted' => FALSE,
    'depth' => 4,
    'data' => array(),
    'values' => FALSE,
    'flat' => FALSE,
  );

  // Do not allow past the maximum token information depth.
  $options['depth'] = min($options['depth'], TOKEN_MAX_DEPTH);

  // If $token_type is an entity, make sure we are using the actual token type.
  if ($entity_token_type = token_get_entity_mapping('entity', $token_type)) {
    $token_type = $entity_token_type;
  }

  $tree_cid = "tree:{$token_type}:{$language->language}:{$options['depth']}";

  // If we do not have this base tree in the static cache, check {cache_token}
  // otherwise generate and store it in the cache.
  if (!isset($trees[$tree_cid])) {
    if ($cache = cache_get($tree_cid, 'cache_token')) {
      $trees[$tree_cid] = $cache->data;
    }
    else {
      $options['parents'] = array();
      $trees[$tree_cid] = _token_build_tree($token_type, $options);
      cache_set($tree_cid, $trees[$tree_cid], 'cache_token');
    }
  }

  $tree = $trees[$tree_cid];

  // If the user has requested a flat tree, convert it.
  if (!empty($options['flat'])) {
    $tree = token_flatten_tree($tree);
  }

  // Fill in token values.
  if (!empty($options['values'])) {
    $token_values = array();
    foreach ($tree as $token => $token_info) {
      if (!empty($token_info['dynamic']) || !empty($token_info['restricted'])) {
        continue;
      }
      elseif (!isset($token_info['value'])) {
        $token_values[$token_info['token']] = $token;
      }
    }
    if (!empty($token_values)) {
      $token_values = token_generate($token_type, $token_values, $options['data']);
      foreach ($token_values as $token => $replacement) {
        $tree[$token]['value'] = $replacement;
      }
    }
  }

  return $tree;
}

/**
 * Flatten a token tree.
 */
function token_flatten_tree($tree) {
  $result = array();
  foreach ($tree as $token => $token_info) {
    $result[$token] = $token_info;
    if (isset($token_info['children']) && is_array($token_info['children'])) {
      $result += token_flatten_tree($token_info['children']);
      // unset($result[$token]['children']);
    }
  }
  return $result;
}

/**
 * Generate a token tree.
 */
function _token_build_tree($token_type, array $options) {
  $options += array(
    'parents' => array(),
  );

  $info = token_get_info();
  if ($options['depth'] <= 0 || !isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
    return array();
  }

  $tree = array();
  foreach ($info['tokens'][$token_type] as $token => $token_info) {
    // Build the raw token string.
    $token_parents = $options['parents'];
    if (empty($token_parents)) {
      // If the parents array is currently empty, assume the token type is its
      // parent.
      $token_parents[] = $token_type;
    }
    elseif (in_array($token, array_slice($token_parents, 1))) {
      // Prevent duplicate recursive tokens. For example, this will prevent
      // the tree from generating the following tokens or deeper:
      // [comment:parent:parent]
      // [comment:parent:root:parent]
      continue;
    }

    $token_parents[] = $token;
    if (!empty($token_info['dynamic'])) {
      $token_parents[] = '?';
    }
    $raw_token = '[' . implode(':', $token_parents) . ']';
    $tree[$raw_token] = $token_info;
    $tree[$raw_token]['raw token'] = $raw_token;

    // Add the token's real name (leave out the base token type).
    $tree[$raw_token]['token'] = implode(':', array_slice($token_parents, 1));

    // Add the token's parent as its raw token value.
    if (!empty($options['parents'])) {
      $tree[$raw_token]['parent'] = '[' . implode(':', $options['parents']) . ']';
    }

    // Fetch the child tokens.
    if (!empty($token_info['type'])) {
      $child_options = $options;
      $child_options['depth']--;
      $child_options['parents'] = $token_parents;
      $tree[$raw_token]['children'] = _token_build_tree($token_info['type'], $child_options);
    }
  }

  return $tree;
}

/**
 * Get a translated menu link by its mlid, without access checking.
 *
 * This function is a copy of menu_link_load() but with its own cache and a
 * simpler query to load the link. This also skips normal menu link access
 * checking by using _token_menu_link_translate().
 *
 * @param $mlid
 *   The mlid of the menu item.
 *
 * @return
 *   A menu link translated for rendering.
 *
 * @see menu_link_load()
 * @see _token_menu_link_translate()
 */
function token_menu_link_load($mlid) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (!is_numeric($mlid)) {
    return FALSE;
  }

  if (!isset($cache[$mlid])) {
    $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();
    if (!empty($item)) {
      _token_menu_link_translate($item);
    }
    $cache[$mlid] = $item;
  }

  return $cache[$mlid];
}

/**
 * Get a translated book menu link by its mlid, without access checking.
 *
 * This function is a copy of book_link_load() but with its own cache and a
 * simpler query to load the link. This also skips normal menu link access
 * checking by using _token_menu_link_translate().
 *
 * @param $mlid
 *   The mlid of the book menu item.
 *
 * @return
 *   A book menu link translated for rendering.
 *
 * @see book_link_load()
 * @see _token_menu_link_translate()
 */
function token_book_link_load($mlid) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (!is_numeric($mlid)) {
    return FALSE;
  }

  if (!isset($cache[$mlid])) {
    $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();
    if (!empty($item)) {
      _token_menu_link_translate($item);
    }
    $cache[$mlid] = $item;
  }

  return $cache[$mlid];
}

function _token_menu_link_translate(&$item) {
  $map = array();

  if (!is_array($item['options'])) {
    $item['options'] = unserialize($item['options']);
  }

  if ($item['external']) {
    $item['access'] = 1;
    $item['href'] = $item['link_path'];
    $item['title'] = $item['link_title'];
    $item['localized_options'] = $item['options'];
  }
  else {
    // Complete the path of the menu link with elements from the current path,
    // if it contains dynamic placeholders (%).
    $map = explode('/', $item['link_path']);
    if (strpos($item['link_path'], '%') !== FALSE) {
      // Invoke registered to_arg callbacks.
      if (!empty($item['to_arg_functions'])) {
        _menu_link_map_translate($map, $item['to_arg_functions']);
      }
    }
    $item['href'] = implode('/', $map);

    // Skip links containing untranslated arguments.
    if (strpos($item['href'], '%') !== FALSE) {
      $item['access'] = FALSE;
      return FALSE;
    }

    $item['access'] = TRUE;
    _menu_item_localize($item, $map, TRUE);
  }

  // Allow other customizations - e.g. adding a page-specific query string to the
  // options array. For performance reasons we only invoke this hook if the link
  // has the 'alter' flag set in the options array.
  if (!empty($item['options']['alter'])) {
    drupal_alter('translated_menu_link', $item, $map);
  }

  return $map;
}

/**
 * Prepare a string for use as a valid token name.
 *
 * @param $name
 *   The token name to clean.
 * @return
 *   The cleaned token name.
 */
function token_clean_token_name($name) {
  static $names = array();

  if (!isset($names[$name])) {
    $cleaned_name = strtr($name, array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
    $cleaned_name = preg_replace('/[^\w\-]/i', '', $cleaned_name);
    $cleaned_name = trim($cleaned_name, '-');
    $names[$name] = $cleaned_name;
  }

  return $names[$name];
}

/**
 * Do not use this function yet. Its API has not been finalized.
 */
function token_render_array(array $array, array $options = array()) {
  $rendered = array();
  foreach (element_children($array) as $key) {
    $value = $array[$key];
    $rendered[] = is_array($value) ? render($value) : (string) $value;
  }
  if (!empty($options['sanitize'])) {
    $rendered = array_map('check_plain', $rendered);
  }
  $join = isset($options['join']) ? $options['join'] : ', ';
  return implode($join, $rendered);
}

/**
 * Do not use this function yet. Its API has not been finalized.
 */
function token_render_array_value($value, array $options = array()) {
  $rendered = is_array($value) ? render($value) : (string) $value;
  if (!empty($options['sanitize'])) {
    $rendered = check_plain($rendered);
  }
  return $rendered;
}

/**
 * Copy of drupal_render_cache_get() that does not care about request method.
 */
function token_render_cache_get($elements) {
  if (!$cid = drupal_render_cid_create($elements)) {
    return FALSE;
  }
  $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';

  if (!empty($cid) && $cache = cache_get($cid, $bin)) {
    // Add additional libraries, JavaScript, CSS and other data attached
    // to this element.
    if (isset($cache->data['#attached'])) {
      drupal_process_attached($cache->data);
    }
    // Return the rendered output.
    return $cache->data['#markup'];
  }
  return FALSE;
}

/**
 * Coyp of drupal_render_cache_set() that does not care about request method.
 */
function token_render_cache_set(&$markup, $elements) {
  // This should only run of drupal_render_cache_set() did not.
  if (in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'))) {
    return FALSE;
  }

  $original_method = $_SERVER['REQUEST_METHOD'];
  $_SERVER['REQUEST_METHOD'] = 'GET';
  drupal_render_cache_set($markup, $elements);
  $_SERVER['REQUEST_METHOD'] = $original_method;
}

function token_menu_link_load_all_parents($mlid) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (!is_numeric($mlid)) {
    return array();
  }

  if (!isset($cache[$mlid])) {
    $cache[$mlid] = array();
    $plid = db_query("SELECT plid FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
    while ($plid && $parent = token_menu_link_load($plid)) {
      $cache[$mlid] = array($plid => $parent['title']) + $cache[$mlid];
      $plid = $parent['plid'];
    }
  }

  return $cache[$mlid];
}

function token_taxonomy_term_load_all_parents($tid) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (!is_numeric($tid)) {
    return array();
  }

  if (!isset($cache[$tid])) {
    $cache[$tid] = array();
    $parents = taxonomy_get_parents_all($tid);
    array_shift($parents); // Remove this term from the array.
    $parents = array_reverse($parents);
    foreach ($parents as $term) {
      $cache[$tid][$term->tid] = entity_label('taxonomy_term', $term);
    }
  }

  return $cache[$tid];
}

/**
 * Load the preferred menu link associated with a node.
 *
 * @param $node
 *   A node object.
 *
 * @return
 *   A menu link array from token_menu_link_load() or FALSE if a menu link was
 *   not found.
 *
 * @see menu_node_prepare()
 * @see token_menu_link_load()
 */
function token_node_menu_link_load($node) {
  $cache = &drupal_static(__FUNCTIon__, array());

  if (!isset($cache[$node->nid])) {
    $mlid = FALSE;

    // Nodes do not have their menu links loaded via menu_node_load().
    if (!isset($node->menu)) {
      // We need to clone the node as menu_node_prepare() may cause data loss.
      // @see http://drupal.org/node/1317926
      $menu_node = clone $node;
      menu_node_prepare($menu_node);
      $mlid = !empty($menu_node->menu['mlid']) ? $menu_node->menu['mlid'] : FALSE;
    }
    else {
      $mlid = !empty($node->menu['mlid']) ? $node->menu['mlid'] : FALSE;
    }

    $cache[$node->nid] = $mlid;
  }

  return $cache[$node->nid] ? token_menu_link_load($cache[$node->nid]) : FALSE;
}