views_handler_field_boolean.inc

  1. 3.x handlers/views_handler_field_boolean.inc
  2. 2.x handlers/views_handler_field_boolean.inc

Definition of views_handler_field_boolean.

File

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