function StringItem::generateSampleValue

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::generateSampleValue()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::generateSampleValue()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::generateSampleValue()

Overrides FieldItemBase::generateSampleValue

1 call to StringItem::generateSampleValue()
StringItemTest::testGenerateSampleValue in core/tests/Drupal/Tests/Core/Field/StringItemTest.php
Tests generating sample values.
4 methods override StringItem::generateSampleValue()
TimeZoneItem::generateSampleValue in core/modules/user/src/TimeZoneItem.php
Generates placeholder field values.
UriItem::generateSampleValue in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php
Generates placeholder field values.
UserNameItem::generateSampleValue in core/modules/user/src/UserNameItem.php
Generates placeholder field values.
UuidItem::generateSampleValue in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php
Generates placeholder field values.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php, line 79

Class

StringItem
Defines the 'string' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
    $max_length = $field_definition->getSetting('max_length');
    // When the maximum length is less than 15, or the field needs to be unique,
    // generate a random word using the maximum length.
    if ($max_length <= 15 || $field_definition->getConstraint('UniqueField')) {
        $values['value'] = ucfirst($random->word($max_length));
        return $values;
    }
    // The minimum length is either 10% of the maximum length, or 15 characters
    // long, whichever is greater.
    $min_length = max(ceil($max_length * 0.1), 15);
    // Reduce the max length to allow us to add a period.
    $max_length -= 1;
    // The random value is generated multiple times to create a slight
    // preference towards values that are closer to the minimum length of the
    // string. For values larger than 255 (which is the default maximum value),
    // the bias towards minimum length is increased. This is because the default
    // maximum length of 255 is often used for fields that include shorter
    // values (i.e. title).
    $length = mt_rand($min_length, mt_rand($min_length, $max_length >= 255 ? mt_rand($min_length, $max_length) : $max_length));
    $string = $random->sentences(1);
    while (mb_strlen($string) < $length) {
        $string .= " {$random->sentences(1)}";
    }
    if (mb_strlen($string) > $max_length) {
        $string = substr($string, 0, $length);
        $string = substr($string, 0, strrpos($string, ' '));
    }
    $string = rtrim($string, ' .');
    // Ensure that the string ends with a full stop if there are multiple
    // sentences.
    $values['value'] = $string . (str_contains($string, '.') ? '.' : '');
    return $values;
}

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