file.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Provide diff field functions for the file module.
  5. */
  6. /**
  7. * Diff field callback for preloading file entities.
  8. */
  9. function file_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  10. $fids = array();
  11. foreach (array_merge_recursive($old_items, $new_items) as $info) {
  12. $fids[$info['fid']] = $info['fid'];
  13. }
  14. $files = file_load_multiple($fids);
  15. foreach ($old_items as $delta => $info) {
  16. $old_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  17. }
  18. foreach ($new_items as $delta => $info) {
  19. $new_items[$delta]['file'] = isset($files[$info['fid']]) ? $files[$info['fid']] : NULL;
  20. }
  21. }
  22. /**
  23. * Diff field callback for parsing file field comparative values.
  24. */
  25. function file_field_diff_view($items, $context) {
  26. $field = $context['field'];
  27. $instance = $context['instance'];
  28. $settings = $context['settings'];
  29. $diff_items = array();
  30. foreach ($items as $delta => $item) {
  31. if (isset($item['file'])) {
  32. $output = array();
  33. // We populate as much as possible to allow the best flexability in any
  34. // string overrides.
  35. $t_args = array();
  36. foreach ($item as $key => $value) {
  37. if (is_scalar($value)) {
  38. $t_args['!' . $key] = $value;
  39. }
  40. }
  41. // Some states do not have the file properties in the item, so put these
  42. // out of the main file object.
  43. if (!empty($item['file'])) {
  44. $file_values = (array) $item['file'];
  45. foreach ($file_values as $key => $value) {
  46. if (is_scalar($value) && !isset($t_args['!' . $key])) {
  47. $t_args['!' . $key] = $value;
  48. }
  49. }
  50. }
  51. $output['file'] = t('File: !filename', $t_args);
  52. if ($settings['compare_description_field'] && !empty($instance['settings']['description_field'])) {
  53. if (!empty($item['description'])) {
  54. $output['description'] = t('Description: !description', $t_args);
  55. }
  56. }
  57. if ($settings['show_id']) {
  58. $output['fid'] = t('File ID: !fid', $t_args);
  59. }
  60. if ($settings['compare_display_field'] && !empty($field['settings']['display_field'])) {
  61. $output['display'] = $item['display'] ? t('Displayed') : t('Hidden');
  62. }
  63. $diff_items[$delta] = implode('; ', $output);
  64. }
  65. }
  66. return $diff_items;
  67. }
  68. /**
  69. * Provide default field comparison options.
  70. */
  71. function file_field_diff_default_options($field_type) {
  72. return array(
  73. 'show_id' => 0,
  74. 'compare_display_field' => 0,
  75. 'compare_description_field' => 0,
  76. );
  77. }
  78. /**
  79. * Provide a form for setting the field comparison options.
  80. */
  81. function file_field_diff_options_form($field_type, $settings) {
  82. $options_form = array();
  83. $options_form['show_id'] = array(
  84. '#type' => 'checkbox',
  85. '#title' => t('Show file ID'),
  86. '#default_value' => $settings['show_id'],
  87. );
  88. $options_form['compare_description_field'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Compare description field'),
  91. '#default_value' => $settings['compare_description_field'],
  92. '#description' => t('This is only used if the "Enable <em>Description</em> field" is checked in the instance settings.'),
  93. );
  94. $options_form['compare_display_field'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Compare display state field'),
  97. '#default_value' => $settings['compare_display_field'],
  98. '#description' => t('This is only used if the "Enable <em>Display</em> field" is checked in the field settings.'),
  99. );
  100. return $options_form;
  101. }