diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 5c21ace87b1b36c03c27ce736dc6fc8a55fec1fc..722a0c1de92f92a8ad573228ac571e52009875ad 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -121,25 +121,30 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { - // Set of possible top-level domains. - $tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info'); - // Set random length for the domain name. - $domain_length = mt_rand(7, 15); $random = new Random(); - - switch ($field_definition->getSetting('title')) { - case DRUPAL_DISABLED: - $values['title'] = ''; - break; - case DRUPAL_REQUIRED: - $values['title'] = $random->sentences(4); - break; - case DRUPAL_OPTIONAL: - // In case of optional title, randomize its generation. - $values['title'] = mt_rand(0,1) ? $random->sentences(4) : ''; - break; + if ($field_definition->getItemDefinition()->getSetting('link_type') & LinkItemInterface::LINK_EXTERNAL) { + // Set of possible top-level domains. + $tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info'); + // Set random length for the domain name. + $domain_length = mt_rand(7, 15); + + switch ($field_definition->getSetting('title')) { + case DRUPAL_DISABLED: + $values['title'] = ''; + break; + case DRUPAL_REQUIRED: + $values['title'] = $random->sentences(4); + break; + case DRUPAL_OPTIONAL: + // In case of optional title, randomize its generation. + $values['title'] = mt_rand(0, 1) ? $random->sentences(4) : ''; + break; + } + $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds) - 1))]; + } + else { + $values['uri'] = 'base:' . $random->name(mt_rand(1, 64)); } - $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))]; return $values; } diff --git a/core/modules/link/src/Tests/LinkItemTest.php b/core/modules/link/src/Tests/LinkItemTest.php index 382a464f102af3a31fa8125c017189a352b74c7f..1115983f9334774b8210ebc92fcdd3389bf3aa43 100644 --- a/core/modules/link/src/Tests/LinkItemTest.php +++ b/core/modules/link/src/Tests/LinkItemTest.php @@ -10,7 +10,10 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\FieldUnitTestBase; +use Drupal\link\LinkItemInterface; /** * Tests the new entity API for the link field type. @@ -29,17 +32,40 @@ class LinkItemTest extends FieldUnitTestBase { protected function setUp() { parent::setUp(); - // Create a link field for validation. - entity_create('field_storage_config', array( + // Create a generic, external, and internal link fields for validation. + FieldStorageConfig::create([ 'field_name' => 'field_test', 'entity_type' => 'entity_test', 'type' => 'link', - ))->save(); - entity_create('field_config', array( + ])->save(); + FieldConfig::create([ 'entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', - ))->save(); + 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC], + ])->save(); + FieldStorageConfig::create([ + 'field_name' => 'field_test_external', + 'entity_type' => 'entity_test', + 'type' => 'link', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test_external', + 'bundle' => 'entity_test', + 'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL], + ])->save(); + FieldStorageConfig::create([ + 'field_name' => 'field_test_internal', + 'entity_type' => 'entity_test', + 'type' => 'link', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test_internal', + 'bundle' => 'entity_test', + 'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL], + ])->save(); } /** @@ -130,9 +156,12 @@ public function testLinkItem() { $entity->field_test[0] = NULL; $this->assertNull($entity->field_test[0]->getValue()); - // Test the generateSampleValue() method. + // Test the generateSampleValue() method for generic, external, and internal + // link types. $entity = entity_create('entity_test'); $entity->field_test->generateSampleItems(); + $entity->field_test_external->generateSampleItems(); + $entity->field_test_internal->generateSampleItems(); $this->entityValidateAndSave($entity); }