3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
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;
15 * Defines the 'boolean' entity field type.
19 * label = @Translation("Boolean"),
20 * description = @Translation("An entity field containing a boolean value."),
21 * default_widget = "boolean_checkbox",
22 * default_formatter = "boolean",
25 class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
30 public static function defaultFieldSettings() {
32 'on_label' => new TranslatableMarkup('On'),
33 'off_label' => new TranslatableMarkup('Off'),
34 ] + parent::defaultFieldSettings();
40 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
41 $properties['value'] = DataDefinition::create('boolean')
42 ->setLabel(t('Boolean value'))
51 public static function schema(FieldStorageDefinitionInterface $field_definition) {
65 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
68 $element['on_label'] = [
69 '#type' => 'textfield',
70 '#title' => $this->t('"On" label'),
71 '#default_value' => $this->getSetting('on_label'),
74 $element['off_label'] = [
75 '#type' => 'textfield',
76 '#title' => $this->t('"Off" label'),
77 '#default_value' => $this->getSetting('off_label'),
87 public function getPossibleValues(AccountInterface $account = NULL) {
94 public function getPossibleOptions(AccountInterface $account = NULL) {
96 0 => $this->getSetting('off_label'),
97 1 => $this->getSetting('on_label'),
104 public function getSettableValues(AccountInterface $account = NULL) {
111 public function getSettableOptions(AccountInterface $account = NULL) {
112 return $this->getPossibleOptions($account);
118 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
119 $values['value'] = mt_rand(0, 1);