- <?php
- * @file
- * Token callbacks for the token_example module.
- */
-
- * Implements hook_token_info().
- *
- * @ingroup token_example
- */
- function token_example_token_info() {
-
-
-
- $info['types']['format'] = array(
- 'name' => t('Text formats'),
- 'description' => t('Tokens related to text formats.'),
- 'needs-data' => 'format',
- );
- $info['types']['default-format'] = array(
- 'name' => t('Default text format'),
- 'description' => t("Tokens related to the currently logged in user's default text format."),
- 'type' => 'format',
- );
-
-
- $info['tokens']['format']['id'] = array(
- 'name' => t('ID'),
- 'description' => t("The unique ID of the text format."),
- );
- $info['tokens']['format']['name'] = array(
- 'name' => t('Name'),
- 'description' => t("The name of the text format."),
- );
-
-
- $info['tokens']['node']['body-format'] = array(
- 'name' => t('Body text format'),
- 'description' => t("The name of the text format used on the node's body field."),
- 'type' => 'format',
- );
-
-
- if (module_exists('comment')) {
- $info['tokens']['comment']['body-format'] = array(
- 'name' => t('Body text format'),
- 'description' => t("The name of the text format used on the comment's body field."),
- 'type' => 'format',
- );
- }
-
- return $info;
- }
-
- * Implements hook_tokens().
- *
- * @ingroup token_example
- */
- function token_example_tokens($type, $tokens, array $data = array(), array $options = array()) {
- $replacements = array();
- $sanitize = !empty($options['sanitize']);
-
-
- if ($type == 'format' && !empty($data['format'])) {
- $format = $data['format'];
-
- foreach ($tokens as $name => $original) {
- switch ($name) {
- case 'id':
-
-
- $replacements[$original] = $format->format;
- break;
-
- case 'name':
-
- $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
- break;
- }
- }
- }
-
-
- if ($type == 'default-format') {
- $default_id = filter_default_format();
- $default_format = filter_format_load($default_id);
- $replacements += token_generate('format', $tokens, array('format' => $default_format), $options);
- }
-
-
- if ($type == 'node' && !empty($data['node'])) {
- $node = $data['node'];
-
- foreach ($tokens as $name => $original) {
- switch ($name) {
- case 'body-format':
- if ($items = field_get_items('node', $node, 'body')) {
- $format = filter_format_load($items[0]['format']);
- $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
- }
- break;
- }
- }
-
-
- if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
- if ($items = field_get_items('node', $node, 'body')) {
- $body_format = filter_format_load($items[0]['format']);
- $replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
- }
- }
- }
-
-
- if ($type == 'comment' && !empty($data['comment'])) {
- $comment = $data['comment'];
-
- foreach ($tokens as $name => $original) {
- switch ($name) {
- case 'body-format':
- if ($items = field_get_items('comment', $comment, 'comment_body')) {
- $format = filter_format_load($items[0]['format']);
- $replacements[$original] = $sanitize ? filter_xss($format->name) : $format->name;
- }
- break;
- }
- }
-
-
- if ($format_tokens = token_find_with_prefix($tokens, 'body-format')) {
- if ($items = field_get_items('comment', $comment, 'comment_body')) {
- $body_format = filter_format_load($items[0]['format']);
- $replacements += token_generate('format', $format_tokens, array('format' => $body_format), $options);
- }
- }
- }
-
- return $replacements;
- }