3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Component\Utility\Xss as UtilityXss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Render\ViewsRenderPipelineMarkup;
8 use Drupal\views\ResultRow;
9 use Drupal\views\ViewExecutable;
10 use Drupal\views\Plugin\views\display\DisplayPluginBase;
13 * A handler to provide proper displays for booleans.
15 * Allows for display of true/false, yes/no, on/off, enabled/disabled.
18 * - output formats: An array where the first entry is displayed on boolean true
19 * and the second is displayed on boolean false. An example for sticky is:
21 * 'output formats' => array(
22 * 'sticky' => array(t('Sticky'), ''),
26 * @ingroup views_field_handlers
28 * @ViewsField("boolean")
30 class Boolean extends FieldPluginBase {
35 protected function defineOptions() {
36 $options = parent::defineOptions();
37 $options['type'] = ['default' => 'yes-no'];
38 $options['type_custom_true'] = ['default' => ''];
39 $options['type_custom_false'] = ['default' => ''];
40 $options['not'] = ['default' => FALSE];
48 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
49 parent::init($view, $display, $options);
52 'yes-no' => [t('Yes'), $this->t('No')],
53 'true-false' => [t('True'), $this->t('False')],
54 'on-off' => [t('On'), $this->t('Off')],
55 'enabled-disabled' => [t('Enabled'), $this->t('Disabled')],
57 'unicode-yes-no' => ['✔', '✖'],
59 $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : [];
60 $custom_format = ['custom' => [t('Custom')]];
61 $this->formats = array_merge($default_formats, $output_formats, $custom_format);
67 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
68 foreach ($this->formats as $key => $item) {
69 $options[$key] = implode('/', $item);
74 '#title' => $this->t('Output format'),
75 '#options' => $options,
76 '#default_value' => $this->options['type'],
78 $form['type_custom_true'] = [
79 '#type' => 'textfield',
80 '#title' => $this->t('Custom output for TRUE'),
81 '#default_value' => $this->options['type_custom_true'],
84 'select[name="options[type]"]' => ['value' => 'custom'],
88 $form['type_custom_false'] = [
89 '#type' => 'textfield',
90 '#title' => $this->t('Custom output for FALSE'),
91 '#default_value' => $this->options['type_custom_false'],
94 'select[name="options[type]"]' => ['value' => 'custom'],
99 '#type' => 'checkbox',
100 '#title' => $this->t('Reverse'),
101 '#description' => $this->t('If checked, true will be displayed as false.'),
102 '#default_value' => $this->options['not'],
104 parent::buildOptionsForm($form, $form_state);
110 public function render(ResultRow $values) {
111 $value = $this->getValue($values);
112 if (!empty($this->options['not'])) {
116 if ($this->options['type'] == 'custom') {
117 $custom_value = $value ? $this->options['type_custom_true'] : $this->options['type_custom_false'];
118 return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($custom_value));
120 elseif (isset($this->formats[$this->options['type']])) {
121 return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
124 return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];