Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / Boolean.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
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;
11
12 /**
13  * A handler to provide proper displays for booleans.
14  *
15  * Allows for display of true/false, yes/no, on/off, enabled/disabled.
16  *
17  * Definition terms:
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:
20  *      @code
21  *      'output formats' => array(
22  *        'sticky' => array(t('Sticky'), ''),
23  *      ),
24  *      @endcode
25  *
26  * @ingroup views_field_handlers
27  *
28  * @ViewsField("boolean")
29  */
30 class Boolean extends FieldPluginBase {
31
32   /**
33    * {@inheritdoc}
34    */
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];
41
42     return $options;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
49     parent::init($view, $display, $options);
50
51     $default_formats = [
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')],
56       'boolean' => [1, 0],
57       'unicode-yes-no' => ['✔', '✖'],
58     ];
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);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
68     foreach ($this->formats as $key => $item) {
69       $options[$key] = implode('/', $item);
70     }
71
72     $form['type'] = [
73       '#type' => 'select',
74       '#title' => $this->t('Output format'),
75       '#options' => $options,
76       '#default_value' => $this->options['type'],
77     ];
78     $form['type_custom_true'] = [
79       '#type' => 'textfield',
80       '#title' => $this->t('Custom output for TRUE'),
81       '#default_value' => $this->options['type_custom_true'],
82       '#states' => [
83         'visible' => [
84           'select[name="options[type]"]' => ['value' => 'custom'],
85         ],
86       ],
87     ];
88     $form['type_custom_false'] = [
89       '#type' => 'textfield',
90       '#title' => $this->t('Custom output for FALSE'),
91       '#default_value' => $this->options['type_custom_false'],
92       '#states' => [
93         'visible' => [
94           'select[name="options[type]"]' => ['value' => 'custom'],
95         ],
96       ],
97     ];
98     $form['not'] = [
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'],
103     ];
104     parent::buildOptionsForm($form, $form_state);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function render(ResultRow $values) {
111     $value = $this->getValue($values);
112     if (!empty($this->options['not'])) {
113       $value = !$value;
114     }
115
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));
119     }
120     elseif (isset($this->formats[$this->options['type']])) {
121       return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
122     }
123     else {
124       return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
125     }
126   }
127
128 }