IntegerItem.php 2.95 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\FieldStorageDefinitionInterface;
7
use Drupal\Core\TypedData\DataDefinition;
8
9

/**
10
 * Defines the 'integer' field type.
11
 *
12
13
 * @FieldType(
 *   id = "integer",
14
15
 *   label = @Translation("Number (integer)"),
 *   description = @Translation("This field stores a number in the database as an integer."),
16
 *   category = @Translation("Number"),
17
18
 *   default_widget = "number",
 *   default_formatter = "number_integer"
19
 * )
20
 */
21
class IntegerItem extends NumericItemBase {
22

23
24
25
  /**
   * {@inheritdoc}
   */
26
  public static function defaultStorageSettings() {
27
    return [
28
      'unsigned' => FALSE,
29
30
31
      // Valid size property values include: 'tiny', 'small', 'medium', 'normal'
      // and 'big'.
      'size' => 'normal',
32
    ] + parent::defaultStorageSettings();
33
34
35
36
37
  }

  /**
   * {@inheritdoc}
   */
38
  public static function defaultFieldSettings() {
39
    return [
40
41
42
43
      'min' => '',
      'max' => '',
      'prefix' => '',
      'suffix' => '',
44
    ] + parent::defaultFieldSettings();
45
46
  }

47
  /**
48
   * {@inheritdoc}
49
   */
50
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
51
    $properties['value'] = DataDefinition::create('integer')
52
53
      ->setLabel(t('Integer value'))
      ->setRequired(TRUE);
54

55
    return $properties;
56
  }
57

58
59
60
61
62
63
64
65
66
67
  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    // If this is an unsigned integer, add a validation constraint for the
    // integer to be positive.
    if ($this->getSetting('unsigned')) {
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
68
69
70
      $constraints[] = $constraint_manager->create('ComplexData', [
        'value' => [
          'Range' => [
71
            'min' => 0,
72
            'minMessage' => t('%name: The integer must be larger or equal to %min.', [
73
74
              '%name' => $this->getFieldDefinition()->getLabel(),
              '%min' => 0,
75
76
77
78
            ]),
          ],
        ],
      ]);
79
80
81
82
83
    }

    return $constraints;
  }

84
85
86
  /**
   * {@inheritdoc}
   */
87
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
88
89
90
    return [
      'columns' => [
        'value' => [
91
          'type' => 'int',
92
93
          // Expose the 'unsigned' setting in the field item schema.
          'unsigned' => $field_definition->getSetting('unsigned'),
94
95
96
          // Expose the 'size' setting in the field item schema. For instance,
          // supply 'big' as a value to produce a 'bigint' type.
          'size' => $field_definition->getSetting('size'),
97
98
99
        ],
      ],
    ];
100
101
  }

102
103
104
105
106
107
108
109
110
111
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $min = $field_definition->getSetting('min') ?: 0;
    $max = $field_definition->getSetting('max') ?: 999;
    $values['value'] = mt_rand($min, $max);
    return $values;
  }

112
}