BooleanItem.php 2.88 KB
Newer Older
1 2
<?php

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 18 19
 * @FieldType(
 *   id = "boolean",
 *   label = @Translation("Boolean"),
20
 *   description = @Translation("An entity field containing a boolean value."),
21 22
 *   default_widget = "boolean_checkbox",
 *   default_formatter = "boolean",
23
 * )
24
 */
25
class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
26 27 28 29

  /**
   * {@inheritdoc}
   */
30
  public static function defaultFieldSettings() {
31
    return [
32 33
      'on_label' => new TranslatableMarkup('On'),
      '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 43
      ->setLabel(t('Boolean value'))
      ->setRequired(TRUE);
44

45
    return $properties;
46
  }
47

48 49 50
  /**
   * {@inheritdoc}
   */
51
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
52 53 54
    return [
      'columns' => [
        'value' => [
55 56
          'type' => 'int',
          'size' => 'tiny',
57 58 59
        ],
      ],
    ];
60 61
  }

62 63 64
  /**
   * {@inheritdoc}
   */
65
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
66
    $element = [];
67

68
    $element['on_label'] = [
69 70 71 72
      '#type' => 'textfield',
      '#title' => $this->t('"On" label'),
      '#default_value' => $this->getSetting('on_label'),
      '#required' => TRUE,
73 74
    ];
    $element['off_label'] = [
75 76 77 78
      '#type' => 'textfield',
      '#title' => $this->t('"Off" label'),
      '#default_value' => $this->getSetting('off_label'),
      '#required' => TRUE,
79
    ];
80 81 82 83 84 85 86 87

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleValues(AccountInterface $account = NULL) {
88
    return [0, 1];
89 90 91 92 93 94
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleOptions(AccountInterface $account = NULL) {
95
    return [
96 97
      0 => $this->getSetting('off_label'),
      1 => $this->getSetting('on_label'),
98
    ];
99 100 101 102 103 104
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableValues(AccountInterface $account = NULL) {
105
    return [0, 1];
106 107 108 109 110 111 112 113 114
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableOptions(AccountInterface $account = NULL) {
    return $this->getPossibleOptions($account);
  }

115 116 117 118 119 120 121
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $values['value'] = mt_rand(0, 1);
    return $values;
  }
122

123
}