diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
index 9c97caa..eace5cd 100644
--- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -243,6 +243,28 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// title of the 'uri' element.
if ($this->getFieldSetting('title') == DRUPAL_DISABLED) {
$element['uri']['#title'] = $element['#title'];
+ // By default the field description is added to the title field. Since
+ // the title field is disabled, we add the description, if given, to the
+ // uri element instead.
+ if (!empty($element['#description'])) {
+ if (empty($element['uri']['#description'])) {
+ $element['uri']['#description'] = $element['#description'];
+ }
+ else {
+ // If we have the description of the type of field together with
+ // the user provided description, we want to make a distinction
+ // between "core help text" and "user entered help text". To make
+ // this distinction more clear, we separate them with a line break.
+ $element['uri']['#description'] = [
+ '#type' => 'inline_template',
+ '#template' => '{{ core_description }}
{{ user_description }}',
+ '#context' => [
+ 'core_description' => $element['uri']['#description'],
+ 'user_description' => $element['#description'],
+ ],
+ ];
+ }
+ }
}
// Otherwise wrap everything in a details element.
else {
diff --git a/core/modules/link/src/Tests/LinkFieldUITest.php b/core/modules/link/src/Tests/LinkFieldUITest.php
index c50d6fc..ab0786e 100644
--- a/core/modules/link/src/Tests/LinkFieldUITest.php
+++ b/core/modules/link/src/Tests/LinkFieldUITest.php
@@ -57,9 +57,13 @@ function testFieldUI() {
// Add a link field to the newly-created type. It defaults to allowing both
// internal and external links.
- $label = $this->randomMachineName();
+ $label = 'link_field_label';
+ $description = 'link field description';
$field_name = Unicode::strtolower($label);
- $this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
+ $field_edit = [
+ 'description' => $description,
+ ];
+ $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit);
// Load the formatter page to check that the settings summary does not
// generate warnings.
@@ -67,31 +71,89 @@ function testFieldUI() {
$this->drupalGet("$type_path/display");
$this->assertText(t('Link text trimmed to @limit characters', array('@limit' => 80)));
- // Test the help text displays when the link field allows both internal and
- // external links.
- $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
- $this->drupalGet($add_path);
- $this->assertRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.');
-
- // Log in an admin to set up the next content type.
- $this->drupalLogin($this->adminUser);
-
- // Add a different content type.
- $type = $this->drupalCreateContentType();
- $type_path = 'admin/structure/types/manage/' . $type->id();
- $add_path = 'node/add/' . $type->id();
-
- // Add a link field to the newly-created type. Specify it must allow
- // external only links.
- $label = $this->randomMachineName();
- $field_name = Unicode::strtolower($label);
- $field_edit = ['settings[link_type]' => LinkItemInterface::LINK_EXTERNAL];
- $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit);
-
- // Test the help text displays when link allows only external links.
- $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
- $this->drupalGet($add_path);
- $this->assertRaw('This must be an external URL such as http://example.com.');
+ // There are many combinations of UI settings, where the description should
+ // show: variation on internal, external, both; cardinality (where the
+ // fieldset is hidden or used); and link text shown (required or optional)
+ // or disabled. There are two descriptions: field and URL help text.
+ $cardinalities = [1, 2];
+ $title_settings = [
+ DRUPAL_DISABLED,
+ DRUPAL_OPTIONAL,
+ ];
+ $link_types = [
+ LinkItemInterface::LINK_EXTERNAL,
+ LinkItemInterface::LINK_GENERIC,
+ LinkItemInterface::LINK_INTERNAL,
+ ];
+ $help_texts = [
+ LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as http://example.com.',
+ LinkItemInterface::LINK_GENERIC => 'You can also enter an internal path such as /node/add or an external URL such as http://example.com. Enter <front> to link to the front page.',
+ LinkItemInterface::LINK_INTERNAL => rtrim(\Drupal::url('', array(), array('absolute' => TRUE)), '/'),
+ ];
+ $field_count = 0;
+ // Test all variations of link types on all cardinalities.
+ foreach ($cardinalities as $cardinality) {
+ foreach ($link_types as $link_type) {
+ // Now, test this with both the title enabled and disabled.
+ foreach ($title_settings as $title_setting) {
+ // Both test empty descriptions and not empty descriptions.
+ foreach ([TRUE, FALSE] as $description_enabled) {
+ $field_count++;
+ // Output variation being tested for debugging purpose.
+ $this->verbose("Now testing - cardinality: $cardinality - link_type:
+ $link_type, title_setting: $title_setting, description_enabled:
+ $description_enabled");
+
+ // Log in an admin to set up a content type for this test.
+ $this->drupalLogin($this->adminUser);
+
+ // Add a new content type.
+ $type = $this->drupalCreateContentType();
+ $type_path = 'admin/structure/types/manage/' . $type->id();
+ $add_path = 'node/add/' . $type->id();
+
+ // Create a field for this content type with the current cardinality
+ // and link field settings.
+ $label = "link_field_label_$field_count";
+ $description = 'link field description with html ' . $field_count;
+ $field_name = Unicode::strtolower($label);
+ $storage_edit = ['cardinality_number' => $cardinality];
+ $field_edit = [
+ 'settings[link_type]' => $link_type,
+ 'description' => $description_enabled ? $description : '',
+ 'settings[title]' => $title_setting,
+ ];
+ $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit);
+
+ // Log in a user that is allowed to create this content type, see if
+ // the user can see the expected help text.
+ $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
+ $this->drupalGet($add_path);
+
+ // Check that the help texts we assume should be there, is there.
+ $this->assertRaw($help_texts[$link_type]);
+ if ($link_type === LinkItemInterface::LINK_INTERNAL) {
+ // Internal links have no "system" description. Test that none
+ // of the other help texts show here.
+ $this->assertNoRaw($help_texts[LinkItemInterface::LINK_EXTERNAL]);
+ $this->assertNoRaw($help_texts[LinkItemInterface::LINK_GENERIC]);
+ }
+ // Also assert that the description we made is here, no matter what
+ // the cardinality or link setting.
+ if ($description_enabled) {
+ $this->assertRaw($description);
+ // If the link type was not internal, and the cardinality was 1,
+ // and the title setting was 0, we also check that the field type
+ // help text and the user entered description appear with a
+ // separating them.
+ if (!$title_setting && $cardinality === 1 && $link_type != LinkItemInterface::LINK_INTERNAL) {
+ $this->assertRaw($help_texts[$link_type] . '
' . $description);
+ }
+ }
+ }
+ }
+ }
+ }
}
}
diff --git a/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php b/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php
index 9395d66..33cb4e7 100644
--- a/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php
+++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php
@@ -41,6 +41,8 @@ public function testMenuLinkContentForm() {
$element = $this->xpath('//select[@id = :id]/option[@selected]', array(':id' => 'edit-menu-parent'));
$this->assertTrue($element, 'A default menu parent was found.');
$this->assertEqual('admin:', $element[0]['value'], ' menu is the parent.');
+ // Test that the field description is present.
+ $this->assertRaw('The location this menu link points to.');
$this->drupalPostForm(
NULL,
diff --git a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php
index d034e3b..42047cd 100644
--- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php
+++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php
@@ -64,6 +64,11 @@ public function testShortcutLinkAdd() {
'/admin/config/system/site-information',
];
+ // Test the add shortcut form UI. Test that the base field description is
+ // there.
+ $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
+ $this->assertRaw('The location this shortcut points to.');
+
// Check that each new shortcut links where it should.
foreach ($test_cases as $test_path) {
$title = $this->randomMachineName();