Project

General

Profile

Paste
Download (3.58 KB) Statistics
| Branch: | Revision:

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

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

    
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function option_definition() {
30
    $options = parent::option_definition();
31
    $options['type'] = array('default' => 'yes-no');
32
    $options['type_custom_true'] = array('default' => '', 'translatable' => TRUE);
33
    $options['type_custom_false'] = array('default' => '', 'translatable' => TRUE);
34
    $options['not'] = array('definition bool' => 'reverse');
35

    
36
    return $options;
37
  }
38

    
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function init(&$view, &$options) {
43
    parent::init($view, $options);
44

    
45
    $default_formats = array(
46
      'yes-no' => array(t('Yes'), t('No')),
47
      'true-false' => array(t('True'), t('False')),
48
      'on-off' => array(t('On'), t('Off')),
49
      'enabled-disabled' => array(t('Enabled'), t('Disabled')),
50
      'boolean' => array(1, 0),
51
      'unicode-yes-no' => array('✔', '✖'),
52
    );
53
    $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
54
    $custom_format = array('custom' => array(t('Custom')));
55
    $this->formats = array_merge($default_formats, $output_formats, $custom_format);
56
  }
57

    
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function options_form(&$form, &$form_state) {
62
    foreach ($this->formats as $key => $item) {
63
      $options[$key] = implode('/', $item);
64
    }
65

    
66
    $form['type'] = array(
67
      '#type' => 'select',
68
      '#title' => t('Output format'),
69
      '#options' => $options,
70
      '#default_value' => $this->options['type'],
71
    );
72

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

    
84
    $form['type_custom_false'] = array(
85
      '#type' => 'textfield',
86
      '#title' => t('Custom output for FALSE'),
87
      '#default_value' => $this->options['type_custom_false'],
88
      '#states' => array(
89
        'visible' => array(
90
          'select[name="options[type]"]' => array('value' => 'custom'),
91
        ),
92
      ),
93
    );
94

    
95
    $form['not'] = array(
96
      '#type' => 'checkbox',
97
      '#title' => t('Reverse'),
98
      '#description' => t('If checked, true will be displayed as false.'),
99
      '#default_value' => $this->options['not'],
100
    );
101
    parent::options_form($form, $form_state);
102
  }
103

    
104
  /**
105
   * {@inheritdoc}
106
   */
107
  public function render($values) {
108
    $value = $this->get_value($values);
109
    if (!empty($this->options['not'])) {
110
      $value = !$value;
111
    }
112

    
113
    if ($this->options['type'] == 'custom') {
114
      return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
115
    }
116
    elseif (isset($this->formats[$this->options['type']])) {
117
      return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
118
    }
119
    else {
120
      return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
121
    }
122
  }
123

    
124
}