diff.api.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  106. /**
  107. * Callback to the module that defined the field to prepare items comparison.
  108. *
  109. * This allows the module to alter all items prior to rendering the comparative
  110. * values. It is mainly used to bulk load entities to reduce overheads
  111. * associated with loading entities individually.
  112. *
  113. * @param array $old_items
  114. * An array of field items from the older revision.
  115. * @param array $new_items
  116. * An array of field items from the newer revision.
  117. * @param array $context
  118. * An associative array containing:
  119. * - entity_type: The entity type; e.g., 'node' or 'user'.
  120. * - bundle: The bundle name.
  121. * - field: The field that the items belong to.
  122. * - instance: The instance that the items belong to.
  123. * - language: The language associated with $items.
  124. * - old_entity: The older entity.
  125. * - new_entity: The newer entity.
  126. *
  127. * @see MODULE_field_diff_view()
  128. */
  129. function MODULE_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  130. $fids = array();
  131. foreach (array_merge_recursive($old_items, $new_items) as $info) {
  132. $fids[$info['fid']] = $info['fid'];
  133. }
  134. // A single load is much faster than individual loads.
  135. $files = file_load_multiple($fids);
  136. // For ease of processing, store a reference of the entity on the item array.
  137. foreach ($old_items as $delta => $info) {
  138. $old_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  139. }
  140. foreach ($new_items as $delta => $info) {
  141. $new_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  142. }
  143. }
  144. /**
  145. * Callback to the module that defined the field to generate items comparisons.
  146. *
  147. * @param array $items
  148. * An array of field items from the entity.
  149. * @param array $context
  150. * An associative array containing:
  151. * - entity: The entity being compared.
  152. * - entity_type: The entity type; e.g., 'node' or 'user'.
  153. * - bundle: The bundle name.
  154. * - field: The field that the items belong to.
  155. * - instance: The instance that the items belong to.
  156. * - language: The language associated with $items.
  157. * - old_entity: The older entity.
  158. * - new_entity: The newer entity.
  159. *
  160. * @see MODULE_field_diff_view_prepare()
  161. */
  162. function MODULE_field_diff_view($items, $context) {
  163. $diff_items = array();
  164. foreach ($items as $delta => $item) {
  165. if (isset($item['file'])) {
  166. $diff_items[$delta] = $item['file']->filename . ' [fid: ' . $item['fid'] . ']';
  167. }
  168. }
  169. return $diff_items;
  170. }
  171. /**
  172. * Allow other modules to interact with MODULE_field_diff_view_prepare().
  173. *
  174. * @param array $old_items
  175. * An array of field items from the older revision.
  176. * @param array $new_items
  177. * An array of field items from the newer revision.
  178. * @param array $context
  179. * An associative array containing:
  180. * - entity_type: The entity type; e.g., 'node' or 'user'.
  181. * - bundle: The bundle name.
  182. * - field: The field that the items belong to.
  183. * - instance: The instance that the items belong to.
  184. * - language: The language associated with $items.
  185. * - old_entity: The older entity.
  186. * - new_entity: The newer entity.
  187. *
  188. * @see MODULE_field_diff_view_prepare()
  189. */
  190. function hook_field_diff_view_prepare_alter($old_items, $new_items, $context) {
  191. }
  192. /**
  193. * Allow other modules to interact with MODULE_field_diff_view().
  194. *
  195. * @param array $values
  196. * An array of field items from the entity ready for comparison.
  197. * @param array $items
  198. * An array of field items from the entity.
  199. * @param array $context
  200. * An associative array containing:
  201. * - entity: The entity being compared.
  202. * - entity_type: The entity type; e.g., 'node' or 'user'.
  203. * - bundle: The bundle name.
  204. * - field: The field that the items belong to.
  205. * - instance: The instance that the items belong to.
  206. * - language: The language associated with $items.
  207. * - old_entity: The older entity.
  208. * - new_entity: The newer entity.
  209. *
  210. * @see MODULE_field_diff_view()
  211. */
  212. function hook_field_diff_view_alter($values, $items, $context) {
  213. }
  214. /**
  215. * @} End of "addtogroup hooks".
  216. */