diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 97015d5bef..2126d8e601 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -143,13 +143,34 @@ public static function validateUriElement($element, FormStateInterface $form_sta $uri = static::getUserEnteredStringAsUri($element['#value']); $form_state->setValueForElement($element, $uri); - // If getUserEnteredStringAsUri() mapped the entered value to an 'internal:' - // URI , ensure the raw value begins with '/', '?' or '#'. - // @todo '' is valid input for BC reasons, may be removed by - // https://www.drupal.org/node/2421941 - if (parse_url($uri, PHP_URL_SCHEME) === 'internal' && !in_array($element['#value'][0], ['/', '?', '#'], TRUE) && !str_starts_with($element['#value'], '')) { - $form_state->setError($element, new TranslatableMarkup('Manually entered paths should start with one of the following characters: / ? #')); - return; + // Check if it is an internal link or an external link missing protocol. + if (parse_url($uri, PHP_URL_SCHEME) === 'internal') { + + // Ensure internal links raw value begins with with '/', '?' or '#'. + if (!in_array($element['#value'][0], ['/', '?', '#'], TRUE)) { + + // @todo '' is valid input for BC reasons, may be removed by + // https://www.drupal.org/node/2421941 + if (substr($element['#value'], 0, 7) !== '') { + + // Displays an error message according to the link type. + switch ($element['#link_type']) { + case LinkItemInterface::LINK_EXTERNAL: + $error_message = 'External links must be a full URL and include its protocol, such as %link_example.'; + break; + + case LinkItemInterface::LINK_INTERNAL: + $error_message = 'Use a title of a piece of content to select it, or enter an internal path starting with /, ? or #.'; + break; + + // Error message for LinkItemInterface::LINK_GENERIC. + default: + $error_message = 'Use a title of a piece of content to select it, or enter an internal path starting with /, ? or #. External links must be a full URL and include its protocol, such as %link_example.'; + } + $form_state->setError($element, t($error_message, ['%link_example' => 'http://example.com'])); + return; + } + } } }