token.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @file
  4. * Provide a global context to allow for token support.
  5. */
  6. $plugin = array(
  7. 'title' => t('Token'),
  8. 'description' => t('A context that contains token replacements from token.module.'),
  9. 'context' => 'ctools_context_create_token', // func to create context
  10. 'context name' => 'token',
  11. 'keyword' => 'token',
  12. 'convert list' => 'ctools_context_token_convert_list',
  13. 'convert' => 'ctools_context_token_convert',
  14. );
  15. /**
  16. * Create a context from manual configuration.
  17. *
  18. * @param $empty
  19. * Unused.
  20. * @param $data
  21. * Unused.
  22. * @param $conf
  23. * Unused.
  24. *
  25. * @return ctools_context
  26. * A context of type token, with the plugin set appropriately.
  27. */
  28. function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
  29. $context = new ctools_context('token');
  30. $context->plugin = 'token';
  31. return $context;
  32. }
  33. /**
  34. * Implementation of hook_ctools_context_convert_list().
  35. *
  36. * @return array|null
  37. * An array of token type information, keyed by 'type:id', or NULL if
  38. * none found.
  39. */
  40. function ctools_context_token_convert_list() {
  41. $tokens = token_info();
  42. // Initialise $list here?
  43. foreach ($tokens['types'] as $type => $type_info) {
  44. if (empty($type_info['needs-data'])) {
  45. $real_type = isset($type_info['type']) ? $type_info['type'] : $type;
  46. foreach ($tokens['tokens'][$real_type] as $id => $info) {
  47. $key = "$type:$id";
  48. if (!isset($list[$key])) {
  49. $list[$key] = $type_info['name'] . ': ' . $info['name'];
  50. }
  51. }
  52. }
  53. }
  54. return $list;
  55. }
  56. /**
  57. * Token conversion function: look up the token and return it's value.
  58. *
  59. * @param $context
  60. * Unused.
  61. * @param string $token
  62. * The name of the token.
  63. *
  64. * @return array|null
  65. * The token value, or NULL if not found.
  66. *
  67. * @see ctools_context_convert_context()
  68. */
  69. function ctools_context_token_convert($context, $token) {
  70. $tokens = token_info();
  71. list($type, $token) = explode(':', $token, 2);
  72. $parts = explode(':', $token, 2);
  73. $real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
  74. if (isset($tokens['tokens'][$real_type][$parts[0]])) {
  75. $values = token_generate($type, array($token => $token));
  76. if (isset($values[$token])) {
  77. return $values[$token];
  78. }
  79. }
  80. return NULL;
  81. }