image.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Provide diff field functions for the image module.
  5. */
  6. /**
  7. * Diff field callback for preloading the image file entities.
  8. */
  9. function image_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 image field comparative values.
  24. */
  25. function image_field_diff_view($items, $context) {
  26. $instance = $context['instance'];
  27. $settings = $context['settings'];
  28. $diff_items = array();
  29. foreach ($items as $delta => $item) {
  30. if (isset($item['file'])) {
  31. $output = array();
  32. // We populate as much as possible to allow the best flexability in any
  33. // string overrides.
  34. $t_args = array();
  35. foreach ($item as $key => $value) {
  36. if (is_scalar($value)) {
  37. $t_args['!' . $key] = $value;
  38. }
  39. }
  40. // Some states do not have the file properties in the item, so put these
  41. // out of the main file object.
  42. if (!empty($item['file'])) {
  43. $file_values = (array) $item['file'];
  44. foreach ($file_values as $key => $value) {
  45. if (is_scalar($value) && !isset($t_args['!' . $key])) {
  46. $t_args['!' . $key] = $value;
  47. }
  48. }
  49. }
  50. $output[] = t('Image: !filename', $t_args);
  51. if ($settings['compare_alt_field'] && !empty($instance['settings']['alt_field'])) {
  52. if (!empty($item['alt'])) {
  53. $output[] = t('Alt: !alt', $t_args);
  54. }
  55. }
  56. if ($settings['compare_title_field'] && !empty($instance['settings']['title_field'])) {
  57. if (!empty($item['title'])) {
  58. $output[] = t('Title: !title', $t_args);
  59. }
  60. }
  61. if ($settings['show_id']) {
  62. $output[] = t('File ID: !fid', $t_args);
  63. }
  64. $diff_items[$delta] = implode('; ', $output);
  65. }
  66. }
  67. return $diff_items;
  68. }
  69. /**
  70. * Provide default field comparison options.
  71. */
  72. function image_field_diff_default_options($field_type) {
  73. return array(
  74. 'show_id' => 0,
  75. 'compare_alt_field' => 0,
  76. 'compare_title_field' => 0,
  77. );
  78. }
  79. /**
  80. * Provide a form for setting the field comparison options.
  81. */
  82. function image_field_diff_options_form($field_type, $settings) {
  83. $options_form = array();
  84. $options_form['show_id'] = array(
  85. '#type' => 'checkbox',
  86. '#title' => t('Show image ID'),
  87. '#default_value' => $settings['show_id'],
  88. );
  89. $options_form['compare_alt_field'] = array(
  90. '#type' => 'checkbox',
  91. '#title' => t('Compare <em>Alt</em> field'),
  92. '#default_value' => $settings['compare_alt_field'],
  93. '#description' => t('This is only used if the "Enable <em>Alt</em> field" is checked in the instance settings.'),
  94. );
  95. $options_form['compare_title_field'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Compare <em>Title</em> field'),
  98. '#default_value' => $settings['compare_title_field'],
  99. '#description' => t('This is only used if the "Enable <em>Title</em> field" is checked in the instance settings.'),
  100. );
  101. return $options_form;
  102. }