IntegerItem.php in Drupal 10
File
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class IntegerItem extends NumericItemBase {
public static function defaultStorageSettings() {
return [
'unsigned' => FALSE,
'size' => 'normal',
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
return [
'min' => '',
'max' => '',
'prefix' => '',
'suffix' => '',
] + parent::defaultFieldSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('Integer value'))
->setRequired(TRUE);
return $properties;
}
public function getConstraints() {
$constraints = parent::getConstraints();
if ($this
->getSetting('unsigned')) {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Range' => [
'min' => 0,
'minMessage' => t('%name: The integer must be larger or equal to %min.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'%min' => 0,
]),
],
],
]);
}
return $constraints;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'int',
'unsigned' => $field_definition
->getSetting('unsigned'),
'size' => $field_definition
->getSetting('size'),
],
],
];
}
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;
}
}