views_handler_field_boolean.inc
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @file
* Definition of views_handler_field_boolean.
*/
/**
* A handler to provide proper displays for booleans.
*
* Allows for display of true/false, yes/no, on/off, enabled/disabled.
*
* Definition terms:
* - output formats: An array where the first entry is displayed on boolean true
* and the second is displayed on boolean false. An example for sticky is:
* @code
* 'output formats' => array(
* 'sticky' => array(t('Sticky'), ''),
* ),
* @endcode
*
* @ingroup views_field_handlers
*/
class views_handler_field_boolean extends views_handler_field {
function option_definition() {
$options = parent::option_definition();
$options['type'] = array('default' => 'yes-no');
$options['type_custom_true'] = array('default' => '', 'translatable' => TRUE);
$options['type_custom_false'] = array('default' => '', 'translatable' => TRUE);
$options['not'] = array('definition bool' => 'reverse');
return $options;
}
function init(&$view, &$options) {
parent::init($view, $options);
$default_formats = array(
'yes-no' => array(t('Yes'), t('No')),
'true-false' => array(t('True'), t('False')),
'on-off' => array(t('On'), t('Off')),
'enabled-disabled' => array(t('Enabled'), t('Disabled')),
'boolean' => array(1, 0),
'unicode-yes-no' => array('✔', '✖'),
);
$output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
$custom_format = array('custom' => array(t('Custom')));
$this->formats = array_merge($default_formats, $output_formats, $custom_format);
}
function options_form(&$form, &$form_state) {
foreach ($this->formats as $key => $item) {
$options[$key] = implode('/', $item);
}
$form['type'] = array(
'#type' => 'select',
'#title' => t('Output format'),
'#options' => $options,
'#default_value' => $this->options['type'],
);
$form['type_custom_true'] = array(
'#type' => 'textfield',
'#title' => t('Custom output for TRUE'),
'#default_value' => $this->options['type_custom_true'],
'#states' => array(
'visible' => array(
'select[name="options[type]"]' => array('value' => 'custom'),
),
),
);
$form['type_custom_false'] = array(
'#type' => 'textfield',
'#title' => t('Custom output for FALSE'),
'#default_value' => $this->options['type_custom_false'],
'#states' => array(
'visible' => array(
'select[name="options[type]"]' => array('value' => 'custom'),
),
),
);
$form['not'] = array(
'#type' => 'checkbox',
'#title' => t('Reverse'),
'#description' => t('If checked, true will be displayed as false.'),
'#default_value' => $this->options['not'],
);
parent::options_form($form, $form_state);
}
function render($values) {
$value = $this->get_value($values);
if (!empty($this->options['not'])) {
$value = !$value;
}
if ($this->options['type'] == 'custom') {
return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
}
else if (isset($this->formats[$this->options['type']])) {
return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
}
else {
return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
}
}
}