BooleanFormatter.php 4.5 KB

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