diff.api.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the diff module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Allow modules to provide a comparison about entity properties.
  12. *
  13. * @param object $old_entity
  14. * The older entity revision.
  15. *
  16. * @param object $new_entity
  17. * The newer entity revision.
  18. *
  19. * @param array $context
  20. * An associative array containing:
  21. * - entity_type: The entity type; e.g., 'node' or 'user'.
  22. * - old_entity: The older entity.
  23. * - new_entity: The newer entity.
  24. * - view_mode: The view mode to use. Defaults to FALSE. If no view mode is
  25. * given, the recommended fallback view mode is 'default'.
  26. * - states: An array of view states. These could be one of:
  27. * - raw: The raw value of the diff, the classic 7.x-2.x view.
  28. * - rendered: The rendered HTML as determined by the view mode. Only
  29. * return markup for this state if the value is normally shown
  30. * by this view mode. The user will most likely be able to see
  31. * the raw or raw_plain state, so this is optional.
  32. *
  33. * The rendering state is a work in progress.
  34. *
  35. * Conditionally, you can get these states, but setting these will override
  36. * the user selectable markdown method.
  37. *
  38. * - raw_plain: As raw, but text should be markdowned.
  39. * - rendered_plain: As rendered, but text should be markdowned.
  40. *
  41. * @return array
  42. * An associative array of values keyed by the entity property.
  43. *
  44. * This is effectively an unnested Form API-like structure.
  45. *
  46. * States are returned as follows:
  47. *
  48. * $results['line'] = array(
  49. * '#name' => t('Line'),
  50. * '#states' => array(
  51. * 'raw' => array(
  52. * '#old' => '<p class="line">This was the old line number [tag].</p>',
  53. * '#new' => '<p class="line">This is the new line [tag].</p>',
  54. * ),
  55. * 'rendered' => array(
  56. * '#old' => '<p class="line">This was the old line number <span class="line-number">57</span>.</p>',
  57. * '#new' => '<p class="line">This is the new line <span class="line-number">57</span>.</p>',
  58. * ),
  59. * ),
  60. * );
  61. *
  62. * For backwards compatibility, no changes are required to support states,
  63. * but it is recommended to provide a better UI for end users.
  64. *
  65. * For example, the following example is equivalent to returning the raw
  66. * state from the example above.
  67. *
  68. * $results['line'] = array(
  69. * '#name' => t('Line'),
  70. * '#old' => '<p class="line">This was the old line number [tag].</p>',
  71. * '#new' => '<p class="line">This is the new line [tag].</p>',
  72. * );
  73. */
  74. function hook_entity_diff($old_entity, $new_entity, $context) {
  75. $results = array();
  76. if ($context['entity_type'] == 'node') {
  77. $type = node_type_get_type($new_entity);
  78. $results['title'] = array(
  79. '#name' => $type->title_label,
  80. '#old' => array($old_entity->title),
  81. '#new' => array($new_entity->title),
  82. '#weight' => -5,
  83. '#settings' => array(
  84. 'show_header' => FALSE,
  85. ),
  86. );
  87. }
  88. return $results;
  89. }
  90. /**
  91. * Allow modules to alter a comparison about entities.
  92. *
  93. * @param array $entity_diffs
  94. * An array of entity differences.
  95. * @param array $context
  96. * An associative array containing:
  97. * - entity_type: The entity type; e.g., 'node' or 'user'.
  98. * - old_entity: The older entity.
  99. * - new_entity: The newer entity.
  100. * - view_mode: The view mode to use. Defaults to FALSE.
  101. *
  102. * @see hook_entity_diff()
  103. */
  104. function hook_entity_diff_alter(&$entity_diffs, $context) {
  105. if ($context['entity_type'] == 'node') {
  106. $old_entity = $context['old_entity'];
  107. $new_entity = $context['new_entity'];
  108. $entity_diffs['custom_vid'] = array(
  109. '#name' => t('Second VID'),
  110. '#old' => array($old_entity->vid),
  111. '#new' => array($new_entity->vid),
  112. '#weight' => 5,
  113. );
  114. $entity_diffs['custom_log'] = array(
  115. '#name' => t('Second log'),
  116. '#old' => array($old_entity->log),
  117. '#new' => array($new_entity->log),
  118. '#weight' => 6,
  119. );
  120. }
  121. }
  122. /**
  123. * Callback to the module that defined the field to prepare items comparison.
  124. *
  125. * This allows the module to alter all items prior to rendering the comparative
  126. * values. It is mainly used to bulk load entities to reduce overheads
  127. * associated with loading entities individually.
  128. *
  129. * @param array $old_items
  130. * An array of field items from the older revision.
  131. * @param array $new_items
  132. * An array of field items from the newer revision.
  133. * @param array $context
  134. * An associative array containing:
  135. * - entity_type: The entity type; e.g., 'node' or 'user'.
  136. * - bundle: The bundle name.
  137. * - field: The field that the items belong to.
  138. * - instance: The instance that the items belong to.
  139. * - language: The language associated with $items.
  140. * - old_entity: The older entity.
  141. * - new_entity: The newer entity.
  142. *
  143. * @see MODULE_field_diff_view()
  144. */
  145. function MODULE_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  146. $fids = array();
  147. foreach (array_merge_recursive($old_items, $new_items) as $info) {
  148. $fids[$info['fid']] = $info['fid'];
  149. }
  150. // A single load is much faster than individual loads.
  151. $files = file_load_multiple($fids);
  152. // For ease of processing, store a reference of the entity on the item array.
  153. foreach ($old_items as $delta => $info) {
  154. $old_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  155. }
  156. foreach ($new_items as $delta => $info) {
  157. $new_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  158. }
  159. }
  160. /**
  161. * Callback to the module that defined the field to generate items comparisons.
  162. *
  163. * @param array $items
  164. * An array of field items from the entity.
  165. * @param array $context
  166. * An associative array containing:
  167. * - entity: The entity being compared.
  168. * - entity_type: The entity type; e.g., 'node' or 'user'.
  169. * - bundle: The bundle name.
  170. * - field: The field that the items belong to.
  171. * - instance: The instance that the items belong to.
  172. * - language: The language associated with $items.
  173. * - old_entity: The older entity.
  174. * - new_entity: The newer entity.
  175. *
  176. * @see MODULE_field_diff_view_prepare()
  177. */
  178. function MODULE_field_diff_view($items, $context) {
  179. $diff_items = array();
  180. foreach ($items as $delta => $item) {
  181. if (isset($item['file'])) {
  182. $diff_items[$delta] = $item['file']->filename . ' [fid: ' . $item['fid'] . ']';
  183. }
  184. }
  185. return $diff_items;
  186. }
  187. /**
  188. * Allow other modules to interact with MODULE_field_diff_view_prepare().
  189. *
  190. * @param array $old_items
  191. * An array of field items from the older revision.
  192. * @param array $new_items
  193. * An array of field items from the newer revision.
  194. * @param array $context
  195. * An associative array containing:
  196. * - entity_type: The entity type; e.g., 'node' or 'user'.
  197. * - bundle: The bundle name.
  198. * - field: The field that the items belong to.
  199. * - instance: The instance that the items belong to.
  200. * - language: The language associated with $items.
  201. * - old_entity: The older entity.
  202. * - new_entity: The newer entity.
  203. *
  204. * @see MODULE_field_diff_view_prepare()
  205. */
  206. function hook_field_diff_view_prepare_alter($old_items, $new_items, $context) {
  207. }
  208. /**
  209. * Allow other modules to interact with MODULE_field_diff_view().
  210. *
  211. * @param array $values
  212. * An array of field items from the entity ready for comparison.
  213. * @param array $items
  214. * An array of field items from the entity.
  215. * @param array $context
  216. * An associative array containing:
  217. * - entity: The entity being compared.
  218. * - entity_type: The entity type; e.g., 'node' or 'user'.
  219. * - bundle: The bundle name.
  220. * - field: The field that the items belong to.
  221. * - instance: The instance that the items belong to.
  222. * - language: The language associated with $items.
  223. * - old_entity: The older entity.
  224. * - new_entity: The newer entity.
  225. *
  226. * @see MODULE_field_diff_view()
  227. */
  228. function hook_field_diff_view_alter($values, $items, $context) {
  229. }
  230. /**
  231. * @} End of "addtogroup hooks".
  232. */