You are here

public static function FractionItem::generateSampleValue in Fraction 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/FractionItem.php \Drupal\fraction\Plugin\Field\FieldType\FractionItem::generateSampleValue()

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

src/Plugin/Field/FieldType/FractionItem.php, line 94

Class

FractionItem
Plugin implementation of the 'fraction' field type.

Namespace

Drupal\fraction\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {

  // Generate random decimal (float) with a max of 9 decimal places and then
  // convert it to fraction.
  $divisor = pow(10, rand(0, 9));
  $number = mt_rand(1, 20 * $divisor) / $divisor;
  $fraction = Fraction::createFromDecimal($number);
  return [
    'numerator' => $fraction
      ->getNumerator(),
    'denominator' => $fraction
      ->getDenominator(),
  ];
}