From d16f1dbde8ba1b51310a7d8bc536dadd1342dd93 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Wed, 18 Mar 2015 10:58:48 -0700
Subject: [PATCH] Issue #2421021 by YesCT, lhuacho, martin107,
 shashikant_chauhan, amateescu, tremix: Missing help text for external url
 only for link widget

---
 .../Plugin/Field/FieldWidget/LinkWidget.php   |  5 +++
 .../link/src/Tests/LinkFieldUITest.php        | 43 +++++++++++++++++--
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
index c402f766b1b4..7ed62ba86efc 100644
--- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -208,6 +208,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
     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' => '<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(
       '#type' => 'textfield',
diff --git a/core/modules/link/src/Tests/LinkFieldUITest.php b/core/modules/link/src/Tests/LinkFieldUITest.php
index 847f3c819750..97fd9c8088c7 100644
--- a/core/modules/link/src/Tests/LinkFieldUITest.php
+++ b/core/modules/link/src/Tests/LinkFieldUITest.php
@@ -9,6 +9,7 @@
 
 use Drupal\Component\Utility\Unicode;
 use Drupal\field_ui\Tests\FieldUiTestTrait;
+use Drupal\link\LinkItemInterface;
 use Drupal\simpletest\WebTestBase;
 
 /**
@@ -27,25 +28,35 @@ class LinkFieldUITest extends WebTestBase {
    */
   public static $modules = ['node', 'link', 'field_ui', 'block'];
 
+  /**
+   * A user that can edit content types.
+   *
+   * @var \Drupal\user\UserInterface
+   */
+  protected $adminUser;
+
   /**
    * {@inheritdoc}
    */
   protected function setUp() {
     parent::setUp();
 
-    $this->drupalLogin($this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']));
+    $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
+    $this->drupalLogin($this->adminUser);
     $this->drupalPlaceBlock('system_breadcrumb_block');
   }
 
   /**
-   * Tests that link field UI functionality does not generate warnings.
+   * Tests the link field UI.
    */
   function testFieldUI() {
     // Add a 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.
+    // Add a link field to the newly-created type. It defaults to allowing both
+    // internal and external links.
     $label = $this->randomMachineName();
     $field_name = Unicode::strtolower($label);
     $this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
@@ -55,6 +66,32 @@ function testFieldUI() {
     // @todo Mess with the formatter settings a bit here.
     $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 <em class="placeholder">/node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>.');
+
+    // 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 = ['field[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 <em class="placeholder">http://example.com</em>.');
   }
 
 }
-- 
GitLab