FloatItem.php in Drupal 10
File
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php
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 FloatItem extends NumericItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('float')
->setLabel(t('Float'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'float',
],
],
];
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$element['min']['#step'] = 'any';
$element['max']['#step'] = 'any';
return $element;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$precision = rand(10, 32);
$scale = rand(0, 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;
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
$values['value'] = self::truncateDecimal($random_decimal, $scale);
return $values;
}
}