Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / BooleanFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FormatterBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'boolean' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "boolean",
14  *   label = @Translation("Boolean"),
15  *   field_types = {
16  *     "boolean",
17  *   }
18  * )
19  */
20 class BooleanFormatter extends FormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     $settings = [];
27
28     // Fall back to field settings by default.
29     $settings['format'] = 'default';
30     $settings['format_custom_false'] = '';
31     $settings['format_custom_true'] = '';
32
33     return $settings;
34   }
35
36   /**
37    * Gets the available format options.
38    *
39    * @return array|string
40    *   A list of output formats. Each entry is keyed by the machine name of the
41    *   format. The value is an array, of which the first item is the result for
42    *   boolean TRUE, the second is for boolean FALSE. The value can be also an
43    *   array, but this is just the case for the custom format.
44    */
45   protected function getOutputFormats() {
46     $formats = [
47       'default' => [$this->getFieldSetting('on_label'), $this->getFieldSetting('off_label')],
48       'yes-no' => [$this->t('Yes'), $this->t('No')],
49       'true-false' => [$this->t('True'), $this->t('False')],
50       'on-off' => [$this->t('On'), $this->t('Off')],
51       'enabled-disabled' => [$this->t('Enabled'), $this->t('Disabled')],
52       'boolean' => [1, 0],
53       'unicode-yes-no' => ['✔', '✖'],
54       'custom' => $this->t('Custom'),
55     ];
56
57     return $formats;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function settingsForm(array $form, FormStateInterface $form_state) {
64     $form = parent::settingsForm($form, $form_state);
65
66     $formats = [];
67     foreach ($this->getOutputFormats() as $format_name => $format) {
68       if (is_array($format)) {
69         if ($format_name == 'default') {
70           $formats[$format_name] = $this->t('Field settings (@on_label / @off_label)', ['@on_label' => $format[0], '@off_label' => $format[1]]);
71         }
72         else {
73           $formats[$format_name] = $this->t('@on_label / @off_label', ['@on_label' => $format[0], '@off_label' => $format[1]]);
74         }
75       }
76       else {
77         $formats[$format_name] = $format;
78       }
79     }
80
81     $field_name = $this->fieldDefinition->getName();
82     $form['format'] = [
83       '#type' => 'select',
84       '#title' => $this->t('Output format'),
85       '#default_value' => $this->getSetting('format'),
86       '#options' => $formats,
87     ];
88     $form['format_custom_true'] = [
89       '#type' => 'textfield',
90       '#title' => $this->t('Custom output for TRUE'),
91       '#default_value' => $this->getSetting('format_custom_true'),
92       '#states' => [
93         'visible' => [
94           'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
95         ],
96       ],
97     ];
98     $form['format_custom_false'] = [
99       '#type' => 'textfield',
100       '#title' => $this->t('Custom output for FALSE'),
101       '#default_value' => $this->getSetting('format_custom_false'),
102       '#states' => [
103         'visible' => [
104           'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
105         ],
106       ],
107     ];
108
109     return $form;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function settingsSummary() {
116     $summary = [];
117     $setting = $this->getSetting('format');
118
119     if ($setting == 'custom') {
120       $summary[] = $this->t('Custom text: @true_label / @false_label', [
121         '@true_label' => $this->getSetting('format_custom_true'),
122         '@false_label' => $this->getSetting('format_custom_false'),
123       ]);
124     }
125     else {
126       $formats = $this->getOutputFormats();
127       $summary[] = $this->t('Display: @true_label / @false_label', [
128         '@true_label' => $formats[$setting][0],
129         '@false_label' => $formats[$setting][1],
130       ]);
131     }
132
133     return $summary;
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function viewElements(FieldItemListInterface $items, $langcode) {
140     $elements = [];
141
142     $formats = $this->getOutputFormats();
143
144     foreach ($items as $delta => $item) {
145       $format = $this->getSetting('format');
146
147       if ($format == 'custom') {
148         $elements[$delta] = ['#markup' => $item->value ? $this->getSetting('format_custom_true') : $this->getSetting('format_custom_false')];
149       }
150       else {
151         $elements[$delta] = ['#markup' => $item->value ? $formats[$format][0] : $formats[$format][1]];
152       }
153     }
154
155     return $elements;
156   }
157
158 }