token_example.tokens.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Token callbacks for the token_example module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. *
  9. * @ingroup token_example
  10. */
  11. function token_example_token_info() {
  12. // Add two different token types. The first is the generic text format. The
  13. // second is the user's default text format, which is itself a 'format' token
  14. // type so it can be used directly.
  15. $info['types']['format'] = array(
  16. 'name' => t('Text formats'),
  17. 'description' => t('Tokens related to text formats.'),
  18. 'needs-data' => 'format',
  19. );
  20. $info['types']['default-format'] = array(
  21. 'name' => t('Default text format'),
  22. 'description' => t("Tokens related to the currently logged in user's default text format."),
  23. 'type' => 'format',
  24. );
  25. // Tokens for the text format token type.
  26. $info['tokens']['format']['id'] = array(
  27. 'name' => t('ID'),
  28. 'description' => t("The unique ID of the text format."),
  29. );
  30. $info['tokens']['format']['name'] = array(
  31. 'name' => t('Name'),
  32. 'description' => t("The name of the text format."),
  33. );
  34. // Node tokens.
  35. $info['tokens']['node']['body-format'] = array(
  36. 'name' => t('Body text format'),
  37. 'description' => t("The name of the text format used on the node's body field."),
  38. 'type' => 'format',
  39. );
  40. // Comment tokens.
  41. if (module_exists('comment')) {
  42. $info['tokens']['comment']['body-format'] = array(
  43. 'name' => t('Body text format'),
  44. 'description' => t("The name of the text format used on the comment's body field."),
  45. 'type' => 'format',
  46. );
  47. }
  48. return $info;
  49. }
  50. /**
  51. * Implements hook_tokens().
  52. *
  53. * @ingroup token_example
  54. */
  55. function token_example_tokens($type, $tokens, array $data = array(), array $options = array()) {
  56. $replacements = array();
  57. $sanitize = !empty($options['sanitize']);
  58. // Text format tokens.
  59. if ($type == 'format' && !empty($data['format'])) {
  60. $format = $data['format'];
  61. foreach ($tokens as $name => $original) {
  62. switch ($name) {
  63. case 'id':
  64. // Since {filter_format}.format is an integer and not user-entered
  65. // text, it does not need to ever be sanitized.
  66. $replacements[$original] = $format->format;
  67. break;
  68. case 'name':
  69. // Since the format name is user-entered text, santize when requested.
  70. $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
  71. break;
  72. }
  73. }
  74. }
  75. // Default format tokens.
  76. if ($type == 'default-format') {
  77. $default_id = filter_default_format();
  78. $default_format = filter_format_load($default_id);
  79. $replacements += token_generate('format', $tokens, array('format' => $default_format), $options);
  80. }
  81. // Node tokens.
  82. if ($type == 'node' && !empty($data['node'])) {
  83. $node = $data['node'];
  84. foreach ($tokens as $name => $original) {
  85. switch ($name) {
  86. case 'body-format':
  87. if ($items = field_get_items('node', $node, 'body')) {
  88. $format = filter_format_load($items[0]['format']);
  89. $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
  90. }
  91. break;
  92. }
  93. }
  94. // Chained token relationships.
  95. if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
  96. if ($items = field_get_items('node', $node, 'body')) {
  97. $body_format = filter_format_load($items[0]['format']);
  98. $replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
  99. }
  100. }
  101. }
  102. // Comment tokens.
  103. if ($type == 'comment' && !empty($data['comment'])) {
  104. $comment = $data['comment'];
  105. foreach ($tokens as $name => $original) {
  106. switch ($name) {
  107. case 'body-format':
  108. if ($items = field_get_items('comment', $comment, 'comment_body')) {
  109. $format = filter_format_load($items[0]['format']);
  110. $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
  111. }
  112. break;
  113. }
  114. }
  115. // Chained token relationships.
  116. if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
  117. if ($items = field_get_items('comment', $comment, 'comment_body')) {
  118. $body_format = filter_format_load($items[0]['format']);
  119. $replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
  120. }
  121. }
  122. }
  123. return $replacements;
  124. }