diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 9c97caa..48bd6f4 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -243,6 +243,17 @@ 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 { + $element['uri']['#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..109475b 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,92 @@ 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, + ]; + $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. + switch ($link_type) { + case LinkItemInterface::LINK_GENERIC: + // Check that the text for allowing both internal and external + // is present. + $this->assertRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.'); + break; + + case LinkItemInterface::LINK_EXTERNAL: + // Check the external only link help text. + $this->assertRaw('This must be an external URL such as http://example.com.'); + break; + + case LinkItemInterface::LINK_INTERNAL: + // Internal links have no "system" description. Test that none + // of the above shows here. + $this->assertNoRaw('This must be an external URL such as http://example.com.'); + $this->assertNoRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.'); + // And assert that the field prefix is the internal URL. + $this->assertRaw(rtrim(\Drupal::url('', array(), array('absolute' => TRUE)), '/')); + break; + + } + // Also assert that the description we made is here, no matter what + // the cardinality or link setting. + if ($description_enabled) { + $this->assertRaw($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..7bb8990 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 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 e52b9b6..ad5bc90 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();