taxonomy.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @file
  4. * Implements pusdeo-hook hook_field_diff_view() for the Taxonomy module.
  5. */
  6. /**
  7. * Diff field callback for preloading term entities.
  8. */
  9. function taxonomy_field_diff_view_prepare(&$old_items, &$new_items, $context) {
  10. $tids = array();
  11. foreach (array_merge_recursive($old_items, $new_items) as $info) {
  12. $tids[$info['tid']] = $info['tid'];
  13. }
  14. $terms = taxonomy_term_load_multiple($tids);
  15. foreach ($old_items as $delta => $info) {
  16. $old_items[$delta]['term'] = isset($terms[$info['tid']]) ? $terms[$info['tid']] : NULL;
  17. }
  18. foreach ($new_items as $delta => $info) {
  19. $new_items[$delta]['term'] = isset($terms[$info['tid']]) ? $terms[$info['tid']] : NULL;
  20. }
  21. }
  22. /**
  23. * Diff field callback for parsing term field comparative values.
  24. */
  25. function taxonomy_field_diff_view($items, $context) {
  26. $settings = $context['settings'];
  27. $instance = $context['instance'];
  28. $diff_items = array();
  29. foreach ($items as $delta => $item) {
  30. if (!empty($item['tid'])) {
  31. if ($item['tid'] == 'autocreate') {
  32. $diff_items[$delta] = t('!term_name (new)', array('!term_name' => $item['name']));
  33. }
  34. elseif (empty($item['term'])) {
  35. $diff_items[$delta] = t('Missing term reference (!tid)', array('!tid' => $item['tid']));
  36. }
  37. else {
  38. $output = array();
  39. $output['name'] = $item['term']->name;
  40. if ($settings['show_id']) {
  41. $output['tid'] = t('Term ID: !tid', array('!tid' => $item['term']->tid));
  42. }
  43. $diff_items[$delta] = implode('; ', $output);
  44. }
  45. }
  46. }
  47. if (!empty($settings['sort']) && !empty($diff_items)) {
  48. if ($settings['sort'] == DIFF_SORT_VALUE || $instance['widget']['type'] == 'taxonomy_autocomplete') {
  49. usort($diff_items, 'uasort_taxonomy_field_diff_terms');
  50. }
  51. }
  52. return $diff_items;
  53. }
  54. /**
  55. * Helper function for sorting terms.
  56. */
  57. function uasort_taxonomy_field_diff_terms($a, $b) {
  58. // We need to use t() to test for string overrides.
  59. $missing_text = t('Missing term reference');
  60. $a_missing = strpos($a, $missing_text) === 0;
  61. $b_missing = strpos($b, $missing_text) === 0;
  62. if ($a_missing && $b_missing) {
  63. return strnatcmp($a, $b);
  64. }
  65. elseif ($a_missing xor $b_missing) {
  66. return $a_missing ? 100 : -100;
  67. }
  68. return strnatcmp($a, $b);
  69. }
  70. /**
  71. * Provide default field comparison options.
  72. */
  73. function taxonomy_field_diff_default_options($field_type) {
  74. return array(
  75. 'show_id' => 0,
  76. 'sort' => DIFF_SORT_CUSTOM,
  77. );
  78. }
  79. /**
  80. * Provide a form for setting the field comparison options.
  81. */
  82. function taxonomy_field_diff_options_form($field_type, $settings) {
  83. $options_form = array();
  84. $options_form['show_id'] = array(
  85. '#type' => 'checkbox',
  86. '#title' => t('Show term ID'),
  87. '#default_value' => $settings['show_id'],
  88. );
  89. $options_form['sort'] = array(
  90. '#type' => 'radios',
  91. '#title' => t('Sort'),
  92. '#options' => array(
  93. DIFF_SORT_NONE => t('No sort'),
  94. DIFF_SORT_VALUE => t('Sort'),
  95. DIFF_SORT_CUSTOM => t('Sort if free tagging field'),
  96. ),
  97. '#default_value' => $settings['sort'],
  98. );
  99. return $options_form;
  100. }