1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * A handler to provide proper displays for booleans. |
---|
5 | * |
---|
6 | * Allows for display of true/false, yes/no, on/off, enabled/disabled. |
---|
7 | * |
---|
8 | * Definition terms: |
---|
9 | * - output formats: An array where the first entry is displayed on boolean true |
---|
10 | * and the second is displayed on boolean false. An example for sticky is: |
---|
11 | * @code |
---|
12 | * 'output formats' => array( |
---|
13 | * 'sticky' => array(t('Sticky'), ''), |
---|
14 | * ), |
---|
15 | * @endcode |
---|
16 | * |
---|
17 | * @ingroup views_field_handlers |
---|
18 | */ |
---|
19 | class views_handler_field_boolean extends views_handler_field { |
---|
20 | function option_definition() { |
---|
21 | $options = parent::option_definition(); |
---|
22 | $options['type'] = array('default' => 'yes-no'); |
---|
23 | $options['not'] = array('definition bool' => 'reverse'); |
---|
24 | |
---|
25 | return $options; |
---|
26 | } |
---|
27 | |
---|
28 | function init(&$view, $options) { |
---|
29 | parent::init($view, $options); |
---|
30 | |
---|
31 | $default_formats = array( |
---|
32 | 'yes-no' => array(t('Yes'), t('No')), |
---|
33 | 'true-false' => array(t('True'), t('False')), |
---|
34 | 'on-off' => array(t('On'), t('Off')), |
---|
35 | 'enabled-disabled' => array(t('Enabled'), t('Disabled')), |
---|
36 | 'unicode-yes-no' => array('â', 'â'), |
---|
37 | ); |
---|
38 | $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array(); |
---|
39 | $this->formats = array_merge($default_formats, $output_formats); |
---|
40 | } |
---|
41 | |
---|
42 | function options_form(&$form, &$form_state) { |
---|
43 | parent::options_form($form, $form_state); |
---|
44 | foreach ($this->formats as $key => $item) { |
---|
45 | $options[$key] = implode('/', $item); |
---|
46 | } |
---|
47 | |
---|
48 | $form['type'] = array( |
---|
49 | '#type' => 'select', |
---|
50 | '#title' => t('Output format'), |
---|
51 | '#options' => $options, |
---|
52 | '#default_value' => $this->options['type'], |
---|
53 | ); |
---|
54 | $form['not'] = array( |
---|
55 | '#type' => 'checkbox', |
---|
56 | '#title' => t('Reverse'), |
---|
57 | '#description' => t('If checked, true will be displayed as false.'), |
---|
58 | '#default_value' => $this->options['not'], |
---|
59 | ); |
---|
60 | } |
---|
61 | |
---|
62 | function render($values) { |
---|
63 | $value = $this->get_value($values); |
---|
64 | if (!empty($this->options['not'])) { |
---|
65 | $value = !$value; |
---|
66 | } |
---|
67 | |
---|
68 | if (isset($this->formats[$this->options['type']])) { |
---|
69 | return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1]; |
---|
70 | } |
---|
71 | else { |
---|
72 | return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1]; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|