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 / FieldType / BooleanItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\StringTranslation\TranslatableMarkup;
11 use Drupal\Core\TypedData\OptionsProviderInterface;
12 use Drupal\Core\TypedData\DataDefinition;
13
14 /**
15  * Defines the 'boolean' entity field type.
16  *
17  * @FieldType(
18  *   id = "boolean",
19  *   label = @Translation("Boolean"),
20  *   description = @Translation("An entity field containing a boolean value."),
21  *   default_widget = "boolean_checkbox",
22  *   default_formatter = "boolean",
23  * )
24  */
25 class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
26
27   /**
28    * {@inheritdoc}
29    */
30   public static function defaultFieldSettings() {
31     return [
32       'on_label' => new TranslatableMarkup('On'),
33       'off_label' => new TranslatableMarkup('Off'),
34     ] + parent::defaultFieldSettings();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
41     $properties['value'] = DataDefinition::create('boolean')
42       ->setLabel(t('Boolean value'))
43       ->setRequired(TRUE);
44
45     return $properties;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function schema(FieldStorageDefinitionInterface $field_definition) {
52     return [
53       'columns' => [
54         'value' => [
55           'type' => 'int',
56           'size' => 'tiny',
57         ],
58       ],
59     ];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
66     $element = [];
67
68     $element['on_label'] = [
69       '#type' => 'textfield',
70       '#title' => $this->t('"On" label'),
71       '#default_value' => $this->getSetting('on_label'),
72       '#required' => TRUE,
73     ];
74     $element['off_label'] = [
75       '#type' => 'textfield',
76       '#title' => $this->t('"Off" label'),
77       '#default_value' => $this->getSetting('off_label'),
78       '#required' => TRUE,
79     ];
80
81     return $element;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getPossibleValues(AccountInterface $account = NULL) {
88     return [0, 1];
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getPossibleOptions(AccountInterface $account = NULL) {
95     return [
96       0 => $this->getSetting('off_label'),
97       1 => $this->getSetting('on_label'),
98     ];
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getSettableValues(AccountInterface $account = NULL) {
105     return [0, 1];
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getSettableOptions(AccountInterface $account = NULL) {
112     return $this->getPossibleOptions($account);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
119     $values['value'] = mt_rand(0, 1);
120     return $values;
121   }
122
123 }