You are here

public static function UcPriceItem::generateSampleValue in Ubercart 8.4

Generates placeholder field values.

Useful when populating site with placeholder content during site building or profiling.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

Return value

array An associative array of values.

Overrides FieldItemBase::generateSampleValue

File

uc_product/src/Plugin/Field/FieldType/UcPriceItem.php, line 52

Class

UcPriceItem
Defines the Ubercart price field type.

Namespace

Drupal\uc_product\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  $settings = $field_definition
    ->getSettings();
  $precision = 6;
  $scale = 2;
  $max = is_numeric($settings['max']) ?: pow(10, $precision - $scale) - 1;
  $min = is_numeric($settings['min']) ?: -pow(10, $precision - $scale) + 1;

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