diff.tokens.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for diff-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function diff_token_info() {
  10. $node['diff'] = array(
  11. 'name' => t('Latest differences'),
  12. 'description' => t('The differences between the current revision and the previous revision of this node.'),
  13. );
  14. $node['diff-markdown'] = array(
  15. 'name' => t('Latest differences (marked down)'),
  16. 'description' => t('The differences between the current revision and the previous revision of this node, but with a marked-down form of each revision used for comparison.'),
  17. );
  18. return array(
  19. 'tokens' => array('node' => $node),
  20. );
  21. }
  22. /**
  23. * Implements hook_tokens().
  24. */
  25. function diff_tokens($type, $tokens, array $data = array(), array $options = array()) {
  26. $sanitize = !empty($options['sanitize']);
  27. $replacements = array();
  28. if ($type == 'node' && !empty($data['node'])) {
  29. $node = $data['node'];
  30. foreach ($tokens as $name => $original) {
  31. switch ($name) {
  32. // Basic diff standard comparison information.
  33. case 'diff':
  34. case 'diff-markdown':
  35. $revisions = node_revision_list($node);
  36. if (count($revisions) == 1) {
  37. $replacements[$original] = t('(No previous revision available.)');
  38. }
  39. else {
  40. module_load_include('inc', 'diff', 'diff.pages');
  41. $old_vid = _diff_get_previous_vid($revisions, $node->vid);
  42. $state = $name == 'diff' ? 'raw' : 'raw_plain';
  43. $build = diff_diffs_show($node, $old_vid, $node->vid, $state);
  44. unset($build['diff_table']['#rows']['states']);
  45. unset($build['diff_table']['#rows']['navigation']);
  46. unset($build['diff_preview']);
  47. $output = drupal_render_children($build);
  48. if ($sanitize) {
  49. $output = filter_xss($output, array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'table', 'tr', 'th', 'td'));
  50. }
  51. $replacements[$original] = $output;
  52. }
  53. break;
  54. }
  55. }
  56. }
  57. return $replacements;
  58. }