diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php index 1481d884ee8..9d62405a1df 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Component\Utility\Unicode; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; @@ -77,4 +79,16 @@ class UriItem extends StringItem { return parent::isEmpty(); } + /** + * {@inheritdoc} + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $values = parent::generateSampleValue($field_definition); + $suffix_length = $field_definition->getSetting('max_length') - 7; + foreach ($values as $key => $value) { + $values[$key] = 'http://' . Unicode::substr($value, 0, $suffix_length); + } + return $values; + } + } diff --git a/core/modules/field/src/Tests/Uri/UriItemTest.php b/core/modules/field/src/Tests/Uri/UriItemTest.php index 46ffe0ad558..9dc50a2c6e1 100644 --- a/core/modules/field/src/Tests/Uri/UriItemTest.php +++ b/core/modules/field/src/Tests/Uri/UriItemTest.php @@ -68,6 +68,11 @@ class UriItemTest extends FieldUnitTestBase { 'type' => 'uri', ]) ->save(); + + // Test the generateSampleValue() method. + $entity = entity_create('entity_test'); + $entity->$field_name->generateSampleItems(); + $this->entityValidateAndSave($entity); } }