text.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Provide diff field functions for the Text module.
  5. */
  6. /**
  7. * Diff field callback for parsing text field comparative values.
  8. */
  9. function text_field_diff_view($items, $context) {
  10. $field = $context['field'];
  11. $instance = $context['instance'];
  12. $settings = $context['settings'];
  13. $diff_items = array();
  14. foreach ($items as $delta => $item) {
  15. $diff_items[$delta] = array();
  16. // Compute the format for appending to the label.
  17. $format_text = '';
  18. if ($instance['settings']['text_processing'] && $settings['compare_format']) {
  19. $format_id = empty($item['format']) ? filter_fallback_format() : $item['format'];
  20. if ($format = filter_format_load($format_id)) {
  21. $format_text = $format->name;
  22. }
  23. else {
  24. $format_text = t('Missing format !format', array('!format' => $format_id));
  25. }
  26. }
  27. // Compare the summary fields.
  28. $summary = $field['type'] == 'text_with_summary' && $settings['compare_summary'];
  29. if ($summary) {
  30. // Allow users to optionally clean system specific characters.
  31. if (empty($item['summary'])) {
  32. $diff_items[$delta][] = t('Summary field is empty.');
  33. }
  34. else {
  35. if ($format_text) {
  36. $diff_items[$delta][] = t('Summary (!text_format):', array('!text_format' => $format_text));
  37. }
  38. else {
  39. $diff_items[$delta][] = t('Summary:');
  40. }
  41. $diff_items[$delta][] = diff_normalise_text($item['summary']);
  42. }
  43. }
  44. // Only show label if field has summary displayed.
  45. if ($summary) {
  46. if ($format_text) {
  47. $diff_items[$delta][] = t('Content (!text_format):', array('!text_format' => $format_text));
  48. }
  49. else {
  50. $diff_items[$delta][] = t('Content:');
  51. }
  52. }
  53. // Allow users to optionally clean system specific characters.
  54. $diff_items[$delta][] = diff_normalise_text($item['value']);
  55. // If no summary, append the format selection to the bottom of the screen.
  56. // This prevents adding the "Content (format)" label.
  57. if ($format_text && !$summary) {
  58. $diff_items[$delta][] = t('Text format: !text_format', array('!text_format' => $format_text));
  59. }
  60. $diff_items[$delta] = $diff_items[$delta];
  61. }
  62. return $diff_items;
  63. }
  64. /**
  65. * Provide default field comparison options.
  66. */
  67. function text_field_diff_default_options($field_type) {
  68. // Overrides the global 'markdown' setting which does not escape HTML.
  69. $settings = array(
  70. 'compare_format' => 0,
  71. 'markdown' => 'drupal_html_to_text',
  72. 'line_counter' => '',
  73. );
  74. if ($field_type == 'text_with_summary') {
  75. $settings += array(
  76. 'compare_summary' => 0,
  77. );
  78. }
  79. return $settings;
  80. }
  81. /**
  82. * Provide a form for setting the field comparison options.
  83. */
  84. function text_field_diff_options_form($field_type, $settings) {
  85. $options_form = array();
  86. $options_form['compare_format'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('Compare format'),
  89. '#default_value' => $settings['compare_format'],
  90. '#description' => t('This is only used if the "Text processing" instance settings are set to <em>Filtered text (user selects text format)</em>.'),
  91. );
  92. if ($field_type == 'text_with_summary') {
  93. $options_form['compare_summary'] = array(
  94. '#type' => 'checkbox',
  95. '#title' => t('Compare summary separately'),
  96. '#default_value' => $settings['compare_summary'],
  97. '#description' => t('This is only used if the "Summary input" option is checked in the instance settings.'),
  98. );
  99. }
  100. return $options_form;
  101. }