views_handler_field_boolean.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  24. * {@inheritdoc}
  25. */
  26. public function option_definition() {
  27. $options = parent::option_definition();
  28. $options['type'] = array('default' => 'yes-no');
  29. $options['type_custom_true'] = array('default' => '', 'translatable' => TRUE);
  30. $options['type_custom_false'] = array('default' => '', 'translatable' => TRUE);
  31. $options['not'] = array('definition bool' => 'reverse');
  32. return $options;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function init(&$view, &$options) {
  38. parent::init($view, $options);
  39. $default_formats = array(
  40. 'yes-no' => array(t('Yes'), t('No')),
  41. 'true-false' => array(t('True'), t('False')),
  42. 'on-off' => array(t('On'), t('Off')),
  43. 'enabled-disabled' => array(t('Enabled'), t('Disabled')),
  44. 'boolean' => array(1, 0),
  45. 'unicode-yes-no' => array('✔', '✖'),
  46. );
  47. $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
  48. $custom_format = array('custom' => array(t('Custom')));
  49. $this->formats = array_merge($default_formats, $output_formats, $custom_format);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function options_form(&$form, &$form_state) {
  55. foreach ($this->formats as $key => $item) {
  56. $options[$key] = implode('/', $item);
  57. }
  58. $form['type'] = array(
  59. '#type' => 'select',
  60. '#title' => t('Output format'),
  61. '#options' => $options,
  62. '#default_value' => $this->options['type'],
  63. );
  64. $form['type_custom_true'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('Custom output for TRUE'),
  67. '#default_value' => $this->options['type_custom_true'],
  68. '#states' => array(
  69. 'visible' => array(
  70. 'select[name="options[type]"]' => array('value' => 'custom'),
  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. $form['not'] = array(
  85. '#type' => 'checkbox',
  86. '#title' => t('Reverse'),
  87. '#description' => t('If checked, true will be displayed as false.'),
  88. '#default_value' => $this->options['not'],
  89. );
  90. parent::options_form($form, $form_state);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function render($values) {
  96. $value = $this->get_value($values);
  97. if (!empty($this->options['not'])) {
  98. $value = !$value;
  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. elseif (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. }