Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_boolean.inc @ a6e869e4

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_boolean.
6
 */
7

    
8
/**
9
 * A handler to provide proper displays for booleans.
10
 *
11
 * Allows for display of true/false, yes/no, on/off, enabled/disabled.
12
 *
13
 * Definition terms:
14
 *   - output formats: An array where the first entry is displayed on boolean true
15
 *      and the second is displayed on boolean false. An example for sticky is:
16
 *      @code
17
 *      'output formats' => array(
18
 *        'sticky' => array(t('Sticky'), ''),
19
 *      ),
20
 *      @endcode
21
 *
22
 * @ingroup views_field_handlers
23
 */
24
class views_handler_field_boolean extends views_handler_field {
25
  function option_definition() {
26
    $options = parent::option_definition();
27
    $options['type'] = array('default' => 'yes-no');
28
    $options['type_custom_true'] = array('default' => '', 'translatable' => TRUE);
29
    $options['type_custom_false'] = array('default' => '', 'translatable' => TRUE);
30
    $options['not'] = array('definition bool' => 'reverse');
31

    
32
    return $options;
33
  }
34

    
35
  function init(&$view, &$options) {
36
    parent::init($view, $options);
37

    
38
    $default_formats = array(
39
      'yes-no' => array(t('Yes'), t('No')),
40
      'true-false' => array(t('True'), t('False')),
41
      'on-off' => array(t('On'), t('Off')),
42
      'enabled-disabled' => array(t('Enabled'), t('Disabled')),
43
      'boolean' => array(1, 0),
44
      'unicode-yes-no' => array('✔', '✖'),
45
    );
46
    $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
47
    $custom_format = array('custom' => array(t('Custom')));
48
    $this->formats = array_merge($default_formats, $output_formats, $custom_format);
49
  }
50

    
51
  function options_form(&$form, &$form_state) {
52
    foreach ($this->formats as $key => $item) {
53
      $options[$key] = implode('/', $item);
54
    }
55

    
56
    $form['type'] = array(
57
      '#type' => 'select',
58
      '#title' => t('Output format'),
59
      '#options' => $options,
60
      '#default_value' => $this->options['type'],
61
    );
62

    
63
    $form['type_custom_true'] = array(
64
      '#type' => 'textfield',
65
      '#title' => t('Custom output for TRUE'),
66
      '#default_value' => $this->options['type_custom_true'],
67
      '#states' => array(
68
        'visible' => array(
69
          'select[name="options[type]"]' => array('value' => 'custom'),
70
        ),
71
      ),
72
    );
73

    
74
    $form['type_custom_false'] = array(
75
      '#type' => 'textfield',
76
      '#title' => t('Custom output for FALSE'),
77
      '#default_value' => $this->options['type_custom_false'],
78
      '#states' => array(
79
        'visible' => array(
80
          'select[name="options[type]"]' => array('value' => 'custom'),
81
        ),
82
      ),
83
    );
84

    
85
    $form['not'] = array(
86
      '#type' => 'checkbox',
87
      '#title' => t('Reverse'),
88
      '#description' => t('If checked, true will be displayed as false.'),
89
      '#default_value' => $this->options['not'],
90
    );
91
    parent::options_form($form, $form_state);
92
  }
93

    
94
  function render($values) {
95
    $value = $this->get_value($values);
96
    if (!empty($this->options['not'])) {
97
      $value = !$value;
98
    }
99

    
100
    if ($this->options['type'] == 'custom') {
101
      return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
102
    }
103
    else if (isset($this->formats[$this->options['type']])) {
104
      return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
105
    }
106
    else {
107
      return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
108
    }
109
  }
110
}