node.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @file
  4. * Provide diff functions for the node module.
  5. */
  6. /**
  7. * Implements hook_entity_diff().
  8. *
  9. * This function compares core node properties. This is currently limited to:
  10. * - title: The title of the node.
  11. *
  12. * @param object $old_node
  13. * The older node revision.
  14. * @param object $new_node
  15. * The newer node revision.
  16. * @param array $context
  17. * An associative array containing:
  18. * - entity_type: The entity type; e.g., 'node' or 'user'.
  19. * - old_entity: The older entity.
  20. * - new_entity: The newer entity.
  21. * - view_mode: The view mode to use. Defaults to FALSE. If no view mode is
  22. * given, the recommended fallback view mode is 'default'.
  23. * - states: An array of view states. These could be one of:
  24. * - raw: The raw value of the diff, the classic 7.x-2.x view.
  25. * - rendered: The rendered HTML as determined by the view mode. Only
  26. * return markup for this state if the value is normally shown
  27. * by this view mode. The user will most likely be able to see
  28. * the raw or raw_plain state, so this is optional.
  29. *
  30. * The rendering state is a work in progress.
  31. *
  32. * Conditionally, you can get these states, but setting these will override
  33. * the user selectable markdown method.
  34. *
  35. * - raw_plain: As raw, but text should be markdowned.
  36. * - rendered_plain: As rendered, but text should be markdowned.
  37. *
  38. * @return array
  39. * An associative array of values keyed by the entity property.
  40. *
  41. * This is effectively an unnested Form API-like structure.
  42. *
  43. * States are returned as follows:
  44. *
  45. * $results['line'] = array(
  46. * '#name' => t('Line'),
  47. * '#states' => array(
  48. * 'raw' => array(
  49. * '#old' => '<p class="line">This was the old line number [tag].</p>',
  50. * '#new' => '<p class="line">This is the new line [tag].</p>',
  51. * ),
  52. * 'rendered' => array(
  53. * '#old' => '<p class="line">This was the old line number <span class="line-number">57</span>.</p>',
  54. * '#new' => '<p class="line">This is the new line <span class="line-number">57</span>.</p>',
  55. * ),
  56. * ),
  57. * );
  58. *
  59. * For backwards compatability, no changes are required to support states,
  60. * but it is recommended to provide a better UI for end users.
  61. *
  62. * For example, the following example is equivalent to returning the raw
  63. * state from the example above.
  64. *
  65. * $results['line'] = array(
  66. * '#name' => t('Line'),
  67. * '#old' => '<p class="line">This was the old line number [tag].</p>',
  68. * '#new' => '<p class="line">This is the new line [tag].</p>',
  69. * );
  70. */
  71. function node_entity_diff($old_node, $new_node, $context) {
  72. $result = array();
  73. if ($context['entity_type'] == 'node') {
  74. $type = node_type_get_type($new_node);
  75. $result['title'] = array(
  76. '#name' => $type->title_label,
  77. '#states' => array(),
  78. '#weight' => -5,
  79. '#settings' => array(
  80. // Global setting - 'diff_show_header_' . $entity_type
  81. 'show_header' => variable_get('diff_show_header_node', 1),
  82. ),
  83. );
  84. foreach ($context['states'] as $state) {
  85. switch ($state) {
  86. case 'rendered':
  87. $result['title']['#states'][$state] = array(
  88. '#old' => l($old_node->title, 'node/' . $old_node->title),
  89. '#new' => l($new_node->title, 'node/' . $new_node->title),
  90. );
  91. break;
  92. // We specify a default so that the title is allows compared.
  93. case 'raw':
  94. default:
  95. $result['title']['#states'][$state] = array(
  96. '#old' => array($old_node->title),
  97. '#new' => array($new_node->title),
  98. );
  99. break;
  100. }
  101. }
  102. }
  103. return $result;
  104. }