file.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $separator = $settings['property_separator'] == 'nl' ? "\n" : $settings['property_separator'];
  64. $diff_items[$delta] = implode($separator, $output);
  65. }
  66. }
  67. return $diff_items;
  68. }
  69. /**
  70. * Provide default field comparison options.
  71. */
  72. function file_field_diff_default_options($field_type) {
  73. return array(
  74. 'show_id' => 0,
  75. 'compare_display_field' => 0,
  76. 'compare_description_field' => 0,
  77. 'property_separator' => '; ',
  78. );
  79. }
  80. /**
  81. * Provide a form for setting the field comparison options.
  82. */
  83. function file_field_diff_options_form($field_type, $settings) {
  84. $options_form = array();
  85. $options_form['show_id'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Show file ID'),
  88. '#default_value' => $settings['show_id'],
  89. );
  90. $options_form['compare_description_field'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Compare description field'),
  93. '#default_value' => $settings['compare_description_field'],
  94. '#description' => t('This is only used if the "Enable <em>Description</em> field" is checked in the instance settings.'),
  95. );
  96. $options_form['compare_display_field'] = array(
  97. '#type' => 'checkbox',
  98. '#title' => t('Compare display state field'),
  99. '#default_value' => $settings['compare_display_field'],
  100. '#description' => t('This is only used if the "Enable <em>Display</em> field" is checked in the field settings.'),
  101. );
  102. $options_form['property_separator'] = array(
  103. '#type' => 'select',
  104. '#title' => t('Property separator'),
  105. '#default_value' => $settings['property_separator'],
  106. '#description' => t('Provides the ability to show properties inline or across multiple lines.'),
  107. '#options' => array(
  108. ', ' => t('Comma (,)'),
  109. '; ' => t('Semicolon (;)'),
  110. ' ' => t('Space'),
  111. 'nl' => t('New line'),
  112. ),
  113. );
  114. // Allow users to set their own separator using variable_set().
  115. if (!isset($options_form['#options'][$settings['property_separator']])) {
  116. $options_form['#options'][$settings['property_separator']] = $settings['property_separator'];
  117. }
  118. return $options_form;
  119. }