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 / IntegerItem.php
1 <?php
2
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  * @FieldType(
13  *   id = "integer",
14  *   label = @Translation("Number (integer)"),
15  *   description = @Translation("This field stores a number in the database as an integer."),
16  *   category = @Translation("Number"),
17  *   default_widget = "number",
18  *   default_formatter = "number_integer"
19  * )
20  */
21 class IntegerItem extends NumericItemBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultStorageSettings() {
27     return [
28       'unsigned' => FALSE,
29       // Valid size property values include: 'tiny', 'small', 'medium', 'normal'
30       // and 'big'.
31       'size' => 'normal',
32     ] + parent::defaultStorageSettings();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function defaultFieldSettings() {
39     return [
40       'min' => '',
41       'max' => '',
42       'prefix' => '',
43       '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       ->setLabel(t('Integer value'))
53       ->setRequired(TRUE);
54
55     return $properties;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getConstraints() {
62     $constraints = parent::getConstraints();
63
64     // If this is an unsigned integer, add a validation constraint for the
65     // integer to be positive.
66     if ($this->getSetting('unsigned')) {
67       $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
68       $constraints[] = $constraint_manager->create('ComplexData', [
69         'value' => [
70           'Range' => [
71             'min' => 0,
72             'minMessage' => t('%name: The integer must be larger or equal to %min.', [
73               '%name' => $this->getFieldDefinition()->getLabel(),
74               '%min' => 0,
75             ]),
76           ],
77         ],
78       ]);
79     }
80
81     return $constraints;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public static function schema(FieldStorageDefinitionInterface $field_definition) {
88     return [
89       'columns' => [
90         'value' => [
91           'type' => 'int',
92           // Expose the 'unsigned' setting in the field item schema.
93           'unsigned' => $field_definition->getSetting('unsigned'),
94           // Expose the 'size' setting in the field item schema. For instance,
95           // supply 'big' as a value to produce a 'bigint' type.
96           'size' => $field_definition->getSetting('size'),
97         ],
98       ],
99     ];
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
106     $min = $field_definition->getSetting('min') ?: 0;
107     $max = $field_definition->getSetting('max') ?: 999;
108     $values['value'] = mt_rand($min, $max);
109     return $values;
110   }
111
112 }