3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FormatterBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Plugin implementation of the 'boolean' formatter.
14 * label = @Translation("Boolean"),
20 class BooleanFormatter extends FormatterBase {
25 public static function defaultSettings() {
28 // Fall back to field settings by default.
29 $settings['format'] = 'default';
30 $settings['format_custom_false'] = '';
31 $settings['format_custom_true'] = '';
37 * Gets the available format options.
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.
45 protected function getOutputFormats() {
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')],
53 'unicode-yes-no' => ['✔', '✖'],
54 'custom' => $this->t('Custom'),
63 public function settingsForm(array $form, FormStateInterface $form_state) {
64 $form = parent::settingsForm($form, $form_state);
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]]);
73 $formats[$format_name] = $this->t('@on_label / @off_label', ['@on_label' => $format[0], '@off_label' => $format[1]]);
77 $formats[$format_name] = $format;
81 $field_name = $this->fieldDefinition->getName();
84 '#title' => $this->t('Output format'),
85 '#default_value' => $this->getSetting('format'),
86 '#options' => $formats,
88 $form['format_custom_true'] = [
89 '#type' => 'textfield',
90 '#title' => $this->t('Custom output for TRUE'),
91 '#default_value' => $this->getSetting('format_custom_true'),
94 'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
98 $form['format_custom_false'] = [
99 '#type' => 'textfield',
100 '#title' => $this->t('Custom output for FALSE'),
101 '#default_value' => $this->getSetting('format_custom_false'),
104 'select[name="fields[' . $field_name . '][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
115 public function settingsSummary() {
117 $setting = $this->getSetting('format');
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'),
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],
139 public function viewElements(FieldItemListInterface $items, $langcode) {
142 $formats = $this->getOutputFormats();
144 foreach ($items as $delta => $item) {
145 $format = $this->getSetting('format');
147 if ($format == 'custom') {
148 $elements[$delta] = ['#markup' => $item->value ? $this->getSetting('format_custom_true') : $this->getSetting('format_custom_false')];
151 $elements[$delta] = ['#markup' => $item->value ? $formats[$format][0] : $formats[$format][1]];