3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Core\TypedData\DataDefinition;
10 * Defines the 'integer' field type.
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"
21 class IntegerItem extends NumericItemBase {
26 public static function defaultStorageSettings() {
29 // Valid size property values include: 'tiny', 'small', 'medium', 'normal'
32 ] + parent::defaultStorageSettings();
38 public static function defaultFieldSettings() {
44 ] + parent::defaultFieldSettings();
50 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
51 $properties['value'] = DataDefinition::create('integer')
52 ->setLabel(t('Integer value'))
61 public function getConstraints() {
62 $constraints = parent::getConstraints();
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', [
72 'minMessage' => t('%name: The integer must be larger or equal to %min.', [
73 '%name' => $this->getFieldDefinition()->getLabel(),
87 public static function schema(FieldStorageDefinitionInterface $field_definition) {
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'),
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);