diff.api.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 entities.
  12. *
  13. * @param object $old_entity
  14. * The older entity revision.
  15. * @param object $new_entity
  16. * The newer entity revision.
  17. * @param array $context
  18. * An associative array containing:
  19. * - entity_type: The entity type; e.g., 'node' or 'user'.
  20. * - view_mode: The view mode to use. Defaults to FALSE.
  21. *
  22. * @return array
  23. * An associative array of values keyed by the entity property.
  24. *
  25. * @todo
  26. * Investiagate options and document these.
  27. */
  28. function hook_entity_diff($old_entity, $new_entity, $context) {
  29. if ($context['entity_type'] == 'node') {
  30. $type = node_type_get_type($new_entity);
  31. $result['title'] = array(
  32. '#name' => $type->title_label,
  33. '#old' => array($old_entity->title),
  34. '#new' => array($new_entity->title),
  35. '#weight' => -5,
  36. '#settings' => array(
  37. 'show_header' => FALSE,
  38. ),
  39. );
  40. }
  41. }
  42. /**
  43. * Allow modules to alter a comparison about entities.
  44. *
  45. * @param array $entity_diffs
  46. * An array of entity differences.
  47. * @param array $context
  48. * An associative array containing:
  49. * - entity_type: The entity type; e.g., 'node' or 'user'.
  50. * - old_entity: The older entity.
  51. * - new_entity: The newer entity.
  52. * - view_mode: The view mode to use. Defaults to FALSE.
  53. *
  54. * @see hook_entity_diff()
  55. */
  56. function hook_entity_diff_alter($entity_diffs, $context) {
  57. }
  58. /**
  59. * Callback to the module that defined the field to prepare items comparison.
  60. *
  61. * This allows the module to alter all items prior to rendering the comparative
  62. * values. It is mainly used to bulk load entities to reduce overheads
  63. * associated with loading entities individually.
  64. *
  65. * @param array $old_items
  66. * An array of field items from the older revision.
  67. * @param array $new_items
  68. * An array of field items from the newer revision.
  69. * @param array $context
  70. * An associative array containing:
  71. * - entity_type: The entity type; e.g., 'node' or 'user'.
  72. * - bundle: The bundle name.
  73. * - field: The field that the items belong to.
  74. * - instance: The instance that the items belong to.
  75. * - language: The language associated with $items.
  76. * - old_entity: The older entity.
  77. * - new_entity: The newer entity.
  78. *
  79. * @see MODULE_field_diff_view()
  80. */
  81. function MODULE_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  82. $fids = array();
  83. foreach (array_merge_recursive($old_items, $new_items) as $info) {
  84. $fids[$info['fid']] = $info['fid'];
  85. }
  86. // A single load is much faster than individual loads.
  87. $files = file_load_multiple($fids);
  88. // For ease of processing, store a reference of the entity on the item array.
  89. foreach ($old_items as $delta => $info) {
  90. $old_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  91. }
  92. foreach ($new_items as $delta => $info) {
  93. $new_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  94. }
  95. }
  96. /**
  97. * Callback to the module that defined the field to generate items comparisons.
  98. *
  99. * @param array $items
  100. * An array of field items from the entity.
  101. * @param array $context
  102. * An associative array containing:
  103. * - entity: The entity being compared.
  104. * - entity_type: The entity type; e.g., 'node' or 'user'.
  105. * - bundle: The bundle name.
  106. * - field: The field that the items belong to.
  107. * - instance: The instance that the items belong to.
  108. * - language: The language associated with $items.
  109. * - old_entity: The older entity.
  110. * - new_entity: The newer entity.
  111. *
  112. * @see MODULE_field_diff_view_prepare()
  113. */
  114. function MODULE_field_diff_view($items, $context) {
  115. $diff_items = array();
  116. foreach ($items as $delta => $item) {
  117. if (isset($item['file'])) {
  118. $diff_items[$delta] = $item['file']->filename . ' [fid: ' . $item['fid'] . ']';
  119. }
  120. }
  121. return $diff_items;
  122. }
  123. /**
  124. * Allow other modules to interact with MODULE_field_diff_view_prepare().
  125. *
  126. * @param array $old_items
  127. * An array of field items from the older revision.
  128. * @param array $new_items
  129. * An array of field items from the newer revision.
  130. * @param array $context
  131. * An associative array containing:
  132. * - entity_type: The entity type; e.g., 'node' or 'user'.
  133. * - bundle: The bundle name.
  134. * - field: The field that the items belong to.
  135. * - instance: The instance that the items belong to.
  136. * - language: The language associated with $items.
  137. * - old_entity: The older entity.
  138. * - new_entity: The newer entity.
  139. *
  140. * @see MODULE_field_diff_view_prepare()
  141. */
  142. function hook_field_diff_view_prepare_alter($old_items, $new_items, $context) {
  143. }
  144. /**
  145. * Allow other modules to interact with MODULE_field_diff_view().
  146. *
  147. * @param array $values
  148. * An array of field items from the entity ready for comparison.
  149. * @param array $items
  150. * An array of field items from the entity.
  151. * @param array $context
  152. * An associative array containing:
  153. * - entity: The entity being compared.
  154. * - entity_type: The entity type; e.g., 'node' or 'user'.
  155. * - bundle: The bundle name.
  156. * - field: The field that the items belong to.
  157. * - instance: The instance that the items belong to.
  158. * - language: The language associated with $items.
  159. * - old_entity: The older entity.
  160. * - new_entity: The newer entity.
  161. *
  162. * @see MODULE_field_diff_view()
  163. */
  164. function hook_field_diff_view_alter($values, $items, $context) {
  165. }
  166. /**
  167. * @} End of "addtogroup hooks".
  168. */