function TextItemBase::generateSampleValue

Same name and namespace in other branches
  1. 9 core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php \Drupal\text\Plugin\Field\FieldType\TextItemBase::generateSampleValue()
  2. 8.9.x core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php \Drupal\text\Plugin\Field\FieldType\TextItemBase::generateSampleValue()
  3. 11.x core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php \Drupal\text\Plugin\Field\FieldType\TextItemBase::generateSampleValue()

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 131

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 = (int) 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;
}

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