diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 7ed62ba..7c48c13 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -170,9 +170,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen /** @var \Drupal\link\LinkItemInterface $item */ $item = $items[$delta]; + // We will customize the form depending on if the title form element is to + // be shown, and what the cardinality is. + $title_is_enabled = $this->getFieldSetting('title') != DRUPAL_DISABLED; + $cardinality_is_one = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1; + $element['uri'] = array( '#type' => 'url', - '#title' => $this->t('URL'), + // When the title is not enabled, we will not use a fieldset and will + // lose the field label. So, use the field label as the uri form element + // label in that case. + '#title' => ($title_is_enabled || !$cardinality_is_one) ? $this->t('URL') : $this->fieldDefinition->getLabel() . ' (' . $this->t('URL') . ')', '#placeholder' => $this->getSetting('placeholder_url'), // The current field value could have been entered by a different user. // However, if it is inaccessible to the current user, do not display it @@ -183,6 +191,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#required' => $element['#required'], ); + // We will be taking off the fieldset since the title is not enabled. Which + // will mean the description from the field will not show when there is + // more than one link. Add the description from the field so it shows. + if ($cardinality_is_one && !$title_is_enabled) { + $element['uri']['#description'] = $this->fieldDefinition->getDescription(); + } + // If the field is configured to support internal links, it cannot use the // 'url' form element and we have to do the validation ourselves. if ($this->supportsInternalLinks()) { @@ -203,15 +218,30 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen if (!$this->supportsExternalLinks()) { $element['uri']['#field_prefix'] = rtrim(\Drupal::url('', array(), array('absolute' => TRUE)), '/'); } - // If the field is configured to allow both internal and external links, - // show a useful description. - elseif ($this->supportsExternalLinks() && $this->supportsInternalLinks()) { - $element['uri']['#description'] = $this->t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page.', array('%front' => '', '%add-node' => '/node/add', '%url' => 'http://example.com')); - } - // If the field is configured to allow only external links, show a useful - // description. - elseif ($this->supportsExternalLinks() && !$this->supportsInternalLinks()) { - $element['uri']['#description'] = $this->t('This must be an external URL such as %url.', array('%url' => 'http://example.com')); + // Internal-only does not need a description, since the field prefix + // accomplishes what help text would. When external is allowed, show a + // useful description. + else { + // If description is not already initialized from the field, initialize + // it. If using the description from the field, add a space before + // appending the description from the uri form element. + if (!isset($element['uri']['#description'])) { + $element['uri']['#description'] = ''; + } + else { + $element['uri']['#description'] .= ' '; + } + + // If the field is configured to allow both internal and external links, + // show a useful description. + if ($this->supportsExternalLinks() && $this->supportsInternalLinks()) { + $element['uri']['#description'] .= $this->t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page.', array('%front' => '', '%add-node' => '/node/add', '%url' => 'http://example.com')); + } + // If the field is configured to allow only external links, show a useful + // description. + elseif ($this->supportsExternalLinks() && !$this->supportsInternalLinks()) { + $element['uri']['#description'] .= $this->t('This must be an external URL such as %url.', array('%url' => 'http://example.com')); + } } $element['title'] = array( @@ -220,7 +250,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#placeholder' => $this->getSetting('placeholder_title'), '#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL, '#maxlength' => 255, - '#access' => $this->getFieldSetting('title') != DRUPAL_DISABLED, + '#access' => $title_is_enabled, ); // Post-process the title field to make it conditionally required if URL is // non-empty. Omit the validation on the field edit form, since the field @@ -238,8 +268,15 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#attributes' => array('class' => array('link-field-widget-attributes')), ); - // If cardinality is 1, ensure a proper label is output for the field. - if ($this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1) { + // If cardinality is 1, a title and URI form elements will mix with other + // form elements and not appear related. Ensure a label is output for the + // link field and the title and URI form elements are grouped by making it + // a fieldset. When the cardinality is greater than one, each set of title + // and URI form elements are already grouped visually, because of the UI + // that allows reordering them. Also, if the title form element is not shown + // we do not need the grouping. So, only make it a fieldset when title is + // enabled. + if ($cardinality_is_one && $title_is_enabled) { // If the link title is disabled, use the field definition label as the // title of the 'uri' element. if ($this->getFieldSetting('title') == DRUPAL_DISABLED) { diff --git a/core/modules/link/src/Tests/LinkFieldUITest.php b/core/modules/link/src/Tests/LinkFieldUITest.php index 97fd9c8..b548aae 100644 --- a/core/modules/link/src/Tests/LinkFieldUITest.php +++ b/core/modules/link/src/Tests/LinkFieldUITest.php @@ -58,8 +58,12 @@ function testFieldUI() { // Add a link field to the newly-created type. It defaults to allowing both // internal and external links. $label = $this->randomMachineName(); + $description = $this->randomMachineName(); $field_name = Unicode::strtolower($label); - $this->fieldUIAddNewField($type_path, $field_name, $label, 'link'); + $field_edit = [ + 'field[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,11 +71,53 @@ 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. + // 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. + + // Test descriptions show with defaults: internal and external, cardinality + // 1 (field set removed), link text shown. + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); $this->drupalGet($add_path); + // Test URL help text is shown for the case internal and external allowed. $this->assertRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test descriptions show when external only, cardinality 1, link text + // shown. + + // 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(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_EXTERNAL, + 'field[description]' => $description, + ]; + $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); + // Test URL help text is shown for the case external only allowed. + $this->assertRaw('This must be an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test descriptions show when internal only, cardinality 1, link text + // shown. // Log in an admin to set up the next content type. $this->drupalLogin($this->adminUser); @@ -84,14 +130,212 @@ function testFieldUI() { // Add a link field to the newly-created type. Specify it must allow // external only links. $label = $this->randomMachineName(); + $description = $this->randomMachineName(); $field_name = Unicode::strtolower($label); - $field_edit = ['field[settings][link_type]' => LinkItemInterface::LINK_EXTERNAL]; + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_INTERNAL, + 'field[description]' => $description, + ]; $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit); + // Test the help text displays when link allows only internal links. + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); + $this->drupalGet($add_path); + // There is no help text for internal only links, an input field prefix is + // used instead. Test for the prefix. + // @todo Assert the prefix. + // Test field help text is shown. + $this->assertRaw($description); + + // Test descriptions show when internal and external, cardinality 2 (URL + // field set is not removed), link text shown. + + // 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. It defaults to allowing both + // internal and external links. + $label = $this->randomMachineName(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $storage_edit = ['field_storage[cardinality_number]' => 2]; + $field_edit = [ + 'field[description]' => $description, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit); + + // Test description shows with defaults: internal and external, cardinality + // 2, link text shown. + + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); + $this->drupalGet($add_path); + // Test URL help text is shown for the case internal and external allowed. + $this->assertRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test description shows when external only, cardinality 2, link text + // shown. + + // 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(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $storage_edit = ['field_storage[cardinality_number]' => 2]; + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_EXTERNAL, + 'field[description]' => $description, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $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); + // Test URL help text is shown for the case external only allowed. $this->assertRaw('This must be an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test description shows when internal only, cardinality 2, link text + // shown. + + // 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(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $storage_edit = ['field_storage[cardinality_number]' => 2]; + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_INTERNAL, + 'field[description]' => $description, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit); + + // Test the help text displays when link allows only internal links. + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); + $this->drupalGet($add_path); + // There is no help text for internal only links, an input field prefix is + // used instead. Test for the prefix. + // @todo Assert the prefix. + // Test field help text is shown. + $this->assertRaw($description); + + // Test description shows when internal and external, cardinality 1 (URL + // field set is removed), link text not shown. + + // 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. It defaults to allowing both + // internal and external links. + $label = $this->randomMachineName(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $field_edit = [ + 'field[description]' => $description, + 'field[settings][title]' => DRUPAL_DISABLED, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit); + + // Test description shows with defaults: internal and external, cardinality + // 1, link text not shown. + + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); + $this->drupalGet($add_path); + // Test URL help text is shown for the case internal and external allowed. + $this->assertRaw('You can also enter an internal path such as /node/add or an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test description shows when external only, cardinality 2, link text + // shown. + + // 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(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_EXTERNAL, + 'field[description]' => $description, + 'field[settings][title]' => DRUPAL_DISABLED, + ]; + $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); + // Test URL help text is shown for the case external only allowed. + $this->assertRaw('This must be an external URL such as http://example.com.'); + // Test field help text is shown. + $this->assertRaw($description); + + // Test description shows when internal only, cardinality 1, link text not + // shown. + + // 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(); + $description = $this->randomMachineName(); + $field_name = Unicode::strtolower($label); + $field_edit = [ + 'field[settings][link_type]' => LinkItemInterface::LINK_INTERNAL, + 'field[description]' => $description, + 'field[settings][title]' => DRUPAL_DISABLED, + ]; + $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit); + + // Test the help text displays when link allows only internal links. + $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content'])); + $this->drupalGet($add_path); + // There is no help text for internal only links, an input field prefix is + // used instead. Test for the prefix. + // @todo Assert the prefix. + // Test field help text is shown. + $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 7129d38..d60cca7 100644 --- a/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php +++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentFormTest.php @@ -42,6 +42,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 bf98504..0d51c54 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -55,6 +55,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();