Projet

Général

Profil

Paste
Télécharger (3,06 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / diff / includes / taxonomy.inc @ a5572547

1
<?php
2

    
3
/**
4
 * @file
5
 * Implements pusdeo-hook hook_field_diff_view() for the Taxonomy module.
6
 */
7

    
8
/**
9
 * Diff field callback for preloading term entities.
10
 */
11
function taxonomy_field_diff_view_prepare(&$old_items, &$new_items, $context) {
12
  $tids = array();
13
  foreach (array_merge_recursive($old_items, $new_items) as $info) {
14
    $tids[$info['tid']] = $info['tid'];
15
  }
16
  $terms = taxonomy_term_load_multiple($tids);
17
  foreach ($old_items as $delta => $info) {
18
    $old_items[$delta]['term'] = isset($terms[$info['tid']]) ? $terms[$info['tid']] : NULL;
19
  }
20
  foreach ($new_items as $delta => $info) {
21
    $new_items[$delta]['term'] = isset($terms[$info['tid']]) ? $terms[$info['tid']] : NULL;
22
  }
23
}
24

    
25
/**
26
 * Diff field callback for parsing term field comparative values.
27
 */
28
function taxonomy_field_diff_view($items, $context) {
29
  $settings = $context['settings'];
30
  $instance = $context['instance'];
31

    
32
  $diff_items = array();
33
  foreach ($items as $delta => $item) {
34
    if (!empty($item['tid'])) {
35
      if ($item['tid'] == 'autocreate') {
36
        $diff_items[$delta] = t('!term_name (new)', array('!term_name' => $item['name']));
37
      }
38
      elseif (empty($item['term'])) {
39
        $diff_items[$delta] = t('Missing term reference (!tid)', array('!tid' => $item['tid']));
40
      }
41
      else {
42
        $output = array();
43
        $output['name'] = $item['term']->name;
44
        if ($settings['show_id']) {
45
          $output['tid'] = t('Term ID: !tid', array('!tid' => $item['term']->tid));
46
        }
47
        $diff_items[$delta] = implode('; ', $output);
48
      }
49
    }
50
  }
51
  if (!empty($settings['sort']) && !empty($diff_items)) {
52
    if ($settings['sort'] == DIFF_SORT_VALUE || $instance['widget']['type'] == 'taxonomy_autocomplete') {
53
      usort($diff_items, 'uasort_taxonomy_field_diff_terms');
54
    }
55
  }
56
  return $diff_items;
57
}
58

    
59
/**
60
 * Helper function for sorting terms.
61
 */
62
function uasort_taxonomy_field_diff_terms($a, $b) {
63
  // We need to use t() to test for string overrides.
64
  $missing_text = t('Missing term reference');
65
  $a_missing = strpos($a, $missing_text) === 0;
66
  $b_missing = strpos($b, $missing_text) === 0;
67
  if ($a_missing && $b_missing) {
68
    return strnatcmp($a, $b);
69
  }
70
  elseif ($a_missing xor $b_missing) {
71
    return $a_missing ? 100 : -100;
72
  }
73
  return strnatcmp($a, $b);
74
}
75

    
76
/**
77
 * Provide default field comparison options.
78
 */
79
function taxonomy_field_diff_default_options($field_type) {
80
  return array(
81
    'show_id' => 0,
82
    'sort' => DIFF_SORT_CUSTOM,
83
  );
84
}
85

    
86
/**
87
 * Provide a form for setting the field comparison options.
88
 */
89
function taxonomy_field_diff_options_form($field_type, $settings) {
90
  $options_form = array();
91
  $options_form['show_id'] = array(
92
    '#type' => 'checkbox',
93
    '#title' => t('Show term ID'),
94
    '#default_value' => $settings['show_id'],
95
  );
96
  $options_form['sort'] = array(
97
    '#type' => 'radios',
98
    '#title' => t('Sort'),
99
    '#options' => array(
100
      DIFF_SORT_NONE => t('No sort'),
101
      DIFF_SORT_VALUE => t('Sort'),
102
      DIFF_SORT_CUSTOM => t('Sort if free tagging field'),
103
    ),
104
    '#default_value' => $settings['sort'],
105
  );
106
  return $options_form;
107
}