views_handler_field_boolean.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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['not'] = array('definition bool' => 'reverse');
  27. return $options;
  28. }
  29. function init(&$view, &$options) {
  30. parent::init($view, $options);
  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. 'boolean' => array(1, 0),
  37. 'unicode-yes-no' => array('✔', '✖'),
  38. );
  39. $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
  40. $this->formats = array_merge($default_formats, $output_formats);
  41. }
  42. function options_form(&$form, &$form_state) {
  43. foreach ($this->formats as $key => $item) {
  44. $options[$key] = implode('/', $item);
  45. }
  46. $form['type'] = array(
  47. '#type' => 'select',
  48. '#title' => t('Output format'),
  49. '#options' => $options,
  50. '#default_value' => $this->options['type'],
  51. );
  52. $form['not'] = array(
  53. '#type' => 'checkbox',
  54. '#title' => t('Reverse'),
  55. '#description' => t('If checked, true will be displayed as false.'),
  56. '#default_value' => $this->options['not'],
  57. );
  58. parent::options_form($form, $form_state);
  59. }
  60. function render($values) {
  61. $value = $this->get_value($values);
  62. if (!empty($this->options['not'])) {
  63. $value = !$value;
  64. }
  65. if (isset($this->formats[$this->options['type']])) {
  66. return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
  67. }
  68. else {
  69. return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
  70. }
  71. }
  72. }