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 / FieldWidget / BooleanCheckboxWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'boolean_checkbox' widget.
11  *
12  * @FieldWidget(
13  *   id = "boolean_checkbox",
14  *   label = @Translation("Single on/off checkbox"),
15  *   field_types = {
16  *     "boolean"
17  *   },
18  *   multiple_values = TRUE
19  * )
20  */
21 class BooleanCheckboxWidget extends WidgetBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultSettings() {
27     return [
28       'display_label' => TRUE,
29     ] + parent::defaultSettings();
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function settingsForm(array $form, FormStateInterface $form_state) {
36     $element['display_label'] = [
37       '#type' => 'checkbox',
38       '#title' => t('Use field label instead of the "On" label as the label.'),
39       '#default_value' => $this->getSetting('display_label'),
40       '#weight' => -1,
41     ];
42     return $element;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function settingsSummary() {
49     $summary = [];
50
51     $display_label = $this->getSetting('display_label');
52     $summary[] = t('Use field label: @display_label', ['@display_label' => ($display_label ? t('Yes') : 'No')]);
53
54     return $summary;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
61     $element['value'] = $element + [
62       '#type' => 'checkbox',
63       '#default_value' => !empty($items[0]->value),
64     ];
65
66     // Override the title from the incoming $element.
67     if ($this->getSetting('display_label')) {
68       $element['value']['#title'] = $this->fieldDefinition->getLabel();
69     }
70     else {
71       $element['value']['#title'] = $this->fieldDefinition->getSetting('on_label');
72     }
73
74     return $element;
75   }
76
77 }