View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
class DecimalItem extends NumericItemBase {
public static function defaultStorageSettings() {
return [
'precision' => 10,
'scale' => 2,
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Decimal value'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'numeric',
'precision' => $field_definition
->getSetting('precision'),
'scale' => $field_definition
->getSetting('scale'),
],
],
];
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$settings = $this
->getSettings();
$element['precision'] = [
'#type' => 'number',
'#title' => t('Precision'),
'#min' => 10,
'#max' => 32,
'#default_value' => $settings['precision'],
'#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
'#disabled' => $has_data,
];
$element['scale'] = [
'#type' => 'number',
'#title' => t('Scale', [], [
'context' => 'decimal places',
]),
'#min' => 0,
'#max' => 10,
'#default_value' => $settings['scale'],
'#description' => t('The number of digits to the right of the decimal.'),
'#disabled' => $has_data,
];
return $element;
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Regex' => [
'pattern' => '/^[+-]?((\\d+(\\.\\d*)?)|(\\.\\d+))$/i',
],
],
]);
return $constraints;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this
->getSettings();
$element['min']['#step'] = pow(0.1, $settings['scale']);
$element['max']['#step'] = pow(0.1, $settings['scale']);
return $element;
}
public function preSave() {
$this->value = round($this->value, $this
->getSetting('scale'));
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$precision = $settings['precision'] ?: 10;
$scale = $settings['scale'] ?: 2;
$max = is_numeric($settings['max']) ? $settings['max'] : pow(10, $precision - $scale) - 1;
$min = is_numeric($settings['min']) ? $settings['min'] : -pow(10, $precision - $scale) + 1;
$decimal_digits = self::getDecimalDigits($max);
$decimal_digits = max(self::getDecimalDigits($min), $decimal_digits);
$scale = rand($decimal_digits, $scale);
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
$values['value'] = self::truncateDecimal($random_decimal, $scale);
return $values;
}
protected static function getDecimalDigits($decimal) {
$digits = 0;
while ($decimal - round($decimal)) {
$decimal *= 10;
$digits++;
}
return $digits;
}
}