image.inc 4.1 KB

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