function FloatItem::generateSampleValue

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php \Drupal\Core\Field\Plugin\Field\FieldType\FloatItem::generateSampleValue()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php \Drupal\Core\Field\Plugin\Field\FieldType\FloatItem::generateSampleValue()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php \Drupal\Core\Field\Plugin\Field\FieldType\FloatItem::generateSampleValue()

Overrides FieldItemBase::generateSampleValue

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php, line 69

Class

FloatItem
Defines the 'float' field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

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;
    // @see "Example #1 Calculate a random floating-point number" in
    // http://php.net/manual/function.mt-getrandmax.php
    $random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    $values['value'] = self::truncateDecimal($random_decimal, $scale);
    return $values;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.