BigIntItem.php in Big Integer 8
File
src/Plugin/Field/FieldType/BigIntItem.php
View source
<?php
namespace Drupal\bigint\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;
class BigIntItem extends NumericItemBase {
public static function defaultStorageSettings() {
return [
'unsigned' => TRUE,
'size' => 'big',
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('BigInt 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;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$settings = $this
->getSettings();
$element['unsigned'] = [
'#type' => 'checkbox',
'#title' => t('Do not allow values less than 0'),
'#default_value' => $settings['unsigned'],
'#description' => t('When checked, the database column will set as unsigned, disallowing negative numbers.'),
];
return $element;
}
}