tripal_views_handler_field_boolean.inc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class tripal_views_handler_field_boolean extends tripal_views_handler_field {
  3. function option_definition() {
  4. $options = parent::option_definition();
  5. $options['type'] = ['default' => 'yes-no'];
  6. $options['type_custom_true'] = ['default' => '', 'translatable' => TRUE];
  7. $options['type_custom_false'] = ['default' => '', 'translatable' => TRUE];
  8. $options['not'] = ['definition bool' => 'reverse'];
  9. return $options;
  10. }
  11. function init(&$view, &$options) {
  12. parent::init($view, $options);
  13. $default_formats = [
  14. 'yes-no' => [t('Yes'), t('No')],
  15. 'true-false' => [t('True'), t('False')],
  16. 'on-off' => [t('On'), t('Off')],
  17. 'enabled-disabled' => [t('Enabled'), t('Disabled')],
  18. 'boolean' => [1, 0],
  19. 'unicode-yes-no' => ['✔', '✖'],
  20. ];
  21. $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : [];
  22. $custom_format = ['custom' => [t('Custom')]];
  23. $this->formats = array_merge($default_formats, $output_formats, $custom_format);
  24. }
  25. function options_form(&$form, &$form_state) {
  26. foreach ($this->formats as $key => $item) {
  27. $options[$key] = implode('/', $item);
  28. }
  29. $form['type'] = [
  30. '#type' => 'select',
  31. '#title' => t('Output format'),
  32. '#options' => $options,
  33. '#default_value' => $this->options['type'],
  34. ];
  35. $form['type_custom_true'] = [
  36. '#type' => 'textfield',
  37. '#title' => t('Custom output for TRUE'),
  38. '#default_value' => $this->options['type_custom_true'],
  39. '#states' => [
  40. 'visible' => [
  41. 'select[name="options[type]"]' => ['value' => 'custom'],
  42. ],
  43. ],
  44. ];
  45. $form['type_custom_false'] = [
  46. '#type' => 'textfield',
  47. '#title' => t('Custom output for FALSE'),
  48. '#default_value' => $this->options['type_custom_false'],
  49. '#states' => [
  50. 'visible' => [
  51. 'select[name="options[type]"]' => ['value' => 'custom'],
  52. ],
  53. ],
  54. ];
  55. $form['not'] = [
  56. '#type' => 'checkbox',
  57. '#title' => t('Reverse'),
  58. '#description' => t('If checked, true will be displayed as false.'),
  59. '#default_value' => $this->options['not'],
  60. ];
  61. parent::options_form($form, $form_state);
  62. }
  63. function render($values) {
  64. $value = $this->get_value($values);
  65. if (!empty($this->options['not'])) {
  66. $value = !$value;
  67. }
  68. if ($this->options['type'] == 'custom') {
  69. return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
  70. }
  71. else {
  72. if (isset($this->formats[$this->options['type']])) {
  73. return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
  74. }
  75. else {
  76. return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
  77. }
  78. }
  79. }
  80. }