You are here

public static function TextItemBase::generateSampleValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php \Drupal\text\Plugin\Field\FieldType\TextItemBase::generateSampleValue()
  2. 10 core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php \Drupal\text\Plugin\Field\FieldType\TextItemBase::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

1 call to TextItemBase::generateSampleValue()
TextItemBaseTest::testTextFieldSampleValue in core/modules/text/tests/src/Kernel/TextItemBaseTest.php
Tests creation of sample values.

File

core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php, line 73

Class

TextItemBase
Base class for 'text' configurable field types.

Namespace

Drupal\text\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  $random = new Random();
  $settings = $field_definition
    ->getSettings();
  if (empty($settings['max_length'])) {

    // Textarea handling
    $value = $random
      ->paragraphs();
  }
  else {

    // Textfield handling.
    $max = ceil($settings['max_length'] / 3);
    $value = substr($random
      ->sentences(mt_rand(1, $max), FALSE), 0, $settings['max_length']);
  }
  $values = [
    'value' => $value,
    'summary' => $value,
    'format' => filter_fallback_format(),
  ];
  return $values;
}