You are here

public static function IngredientItem::generateSampleValue in Recipe 8.2

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 EntityReferenceItem::generateSampleValue

File

modules/ingredient/src/Plugin/Field/FieldType/IngredientItem.php, line 168

Class

IngredientItem
Plugin implementation of the 'ingredient' field type.

Namespace

Drupal\ingredient\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  $random = new Random();

  // Get the ingredient unit keys.
  $unit_keys = array_keys(\Drupal::service('ingredient.unit')
    ->getConfiguredUnits());
  $random_unit_key = mt_rand(0, count($unit_keys) - 1);

  // Generate an ingredient entity.
  $ingredient = \Drupal::entityTypeManager()
    ->getStorage('ingredient')
    ->create([
    'name' => $random
      ->name(10, TRUE),
  ]);
  $values = [
    'target_id' => $ingredient
      ->id(),
    'quantity' => mt_rand(1, 5),
    'unit_key' => $unit_keys[$random_unit_key],
    'note' => $random
      ->word(15),
  ];
  return $values;
}