token.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
  19. $context = new ctools_context('token');
  20. $context->plugin = 'token';
  21. return $context;
  22. }
  23. /**
  24. * Implementation of hook_ctools_context_convert_list().
  25. */
  26. function ctools_context_token_convert_list() {
  27. $tokens = token_info();
  28. foreach ($tokens['types'] as $type => $type_info) {
  29. if (empty($type_info['needs-data'])) {
  30. $real_type = isset($type_info['type']) ? $type_info['type'] : $type;
  31. foreach ($tokens['tokens'][$real_type] as $id => $info) {
  32. $key = "$type:$id";
  33. if (!isset($list[$key])) {
  34. $list[$key] = $type_info['name'] . ': ' . $info['name'];
  35. }
  36. }
  37. }
  38. }
  39. return $list;
  40. }
  41. /**
  42. * Implementation of hook_ctools_context_converter_alter().
  43. */
  44. function ctools_context_token_convert($context, $token) {
  45. $tokens = token_info();
  46. list($type, $token) = explode(':', $token, 2);
  47. $parts = explode(':', $token, 2);
  48. $real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
  49. if (isset($tokens['tokens'][$real_type][$parts[0]])) {
  50. $values = token_generate($type, array($token => $token));
  51. if (isset($values[$token])) {
  52. return $values[$token];
  53. }
  54. }
  55. }