entityreference.diff.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @file
  4. * Provide Diff module field callbacks for the Entity Reference module.
  5. */
  6. /**
  7. * Diff field callback for preloading entities.
  8. */
  9. function entityreference_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  10. $field = $context['field'];
  11. // Build an array of entities ID.
  12. $entity_ids = array();
  13. foreach (array_merge_recursive($old_items, $new_items) as $item) {
  14. $entity_ids[] = $item['target_id'];
  15. }
  16. // Load those entities and loop through them to extract their labels.
  17. $entities = entity_load($field['settings']['target_type'], $entity_ids);
  18. foreach ($old_items as $delta => $info) {
  19. $old_items[$delta]['entity'] = isset($entities[$info['target_id']]) ? $entities[$info['target_id']] : NULL;
  20. }
  21. foreach ($new_items as $delta => $info) {
  22. $new_items[$delta]['entity'] = isset($entities[$info['target_id']]) ? $entities[$info['target_id']] : NULL;
  23. }
  24. }
  25. /**
  26. * Diff field callback for parsing entity field comparative values.
  27. */
  28. function entityreference_field_diff_view($items, $context) {
  29. $field = $context['field'];
  30. $instance = $context['instance'];
  31. $settings = $context['settings'];
  32. $entity_type = $field['settings']['target_type'];
  33. $diff_items = array();
  34. // We populate as much as possible to allow the best flexability in any
  35. // string overrides.
  36. $t_args = array();
  37. $t_args['!entity_type'] = $entity_type;
  38. $entity_info = entity_get_info($entity_type);
  39. $t_args['!entity_type_label'] = $entity_info['label'];
  40. foreach ($items as $delta => $item) {
  41. if (isset($item['entity'])) {
  42. $output = array();
  43. list($id,, $bundle) = entity_extract_ids($entity_type, $item['entity']);
  44. $t_args['!id'] = $id;
  45. $t_args['!bundle'] = $bundle;
  46. $t_args['!diff_entity_label'] = entity_label($entity_type, $item['entity']);
  47. $output['entity'] = t('!diff_entity_label', $t_args);
  48. if ($settings['show_id']) {
  49. $output['id'] = t('ID: !id', $t_args);
  50. }
  51. $diff_items[$delta] = implode('; ', $output);
  52. }
  53. }
  54. return $diff_items;
  55. }
  56. /**
  57. * Provide default field comparison options.
  58. */
  59. function entityreference_field_diff_default_options($field_type) {
  60. return array(
  61. 'show_id' => 0,
  62. );
  63. }
  64. /**
  65. * Provide a form for setting the field comparison options.
  66. */
  67. function entityreference_field_diff_options_form($field_type, $settings) {
  68. $options_form = array();
  69. $options_form['show_id'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Show ID'),
  72. '#default_value' => $settings['show_id'],
  73. );
  74. return $options_form;
  75. }