views_handler_field_boolean.inc

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

File

handlers/views_handler_field_boolean.inc
View source
  1. <?php
  2. /**
  3. * A handler to provide proper displays for booleans.
  4. *
  5. * Allows for display of true/false, yes/no, on/off.
  6. *
  7. * Definition terms:
  8. * - output formats: An array where the first entry is displayed on boolean true
  9. * and the second is displayed on boolean false. An example for sticky is:
  10. * @code
  11. * 'output formats' => array(
  12. * 'sticky' => array(t('Sticky'), ''),
  13. * ),
  14. * @endcode
  15. *
  16. * @ingroup views_field_handlers
  17. */
  18. class views_handler_field_boolean extends views_handler_field {
  19. function option_definition() {
  20. $options = parent::option_definition();
  21. $options['type'] = array('default' => 'yes-no');
  22. $options['not'] = array('definition bool' => 'reverse');
  23. return $options;
  24. }
  25. function init(&$view, $options) {
  26. parent::init($view, $options);
  27. $default_formats = array(
  28. 'yes-no' => array(t('Yes'), t('No')),
  29. 'true-false' => array(t('True'), t('False')),
  30. 'on-off' => array(t('On'), t('Off')),
  31. );
  32. $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
  33. $this->formats = array_merge($default_formats, $output_formats);
  34. }
  35. function options_form(&$form, &$form_state) {
  36. parent::options_form($form, $form_state);
  37. foreach ($this->formats as $key => $item) {
  38. $options[$key] = implode('/', $item);
  39. }
  40. $form['type'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Output format'),
  43. '#options' => $options,
  44. '#default_value' => $this->options['type'],
  45. );
  46. $form['not'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Reverse'),
  49. '#description' => t('If checked, true will be displayed as false.'),
  50. '#default_value' => $this->options['not'],
  51. );
  52. }
  53. function render($values) {
  54. $value = $values->{$this->field_alias};
  55. if (!empty($this->options['not'])) {
  56. $value = !$value;
  57. }
  58. if (isset($this->formats[$this->options['type']])) {
  59. return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
  60. }
  61. else {
  62. return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
  63. }
  64. }
  65. }