diff --git a/core/lib/Drupal/Component/Utility/UrlHelper.php b/core/lib/Drupal/Component/Utility/UrlHelper.php index 691413e..d0e67cb 100644 --- a/core/lib/Drupal/Component/Utility/UrlHelper.php +++ b/core/lib/Drupal/Component/Utility/UrlHelper.php @@ -366,4 +366,24 @@ public static function isValid($url, $absolute = FALSE) { } } + /** + * Returns the internal path from a uri of form user-path://internal/path. + * + * @param string $uri + * The user entered URI + * + * @return string + * The path part of the user-path URI. + * + * @throws \InvalidArgumentException + * When the URI provided does not use the user-path:// scheme. + */ + public static function userPathFromUri($uri) { + $parts = parse_url($uri); + if ($parts['scheme'] !== 'user-path') { + throw new \InvalidArgumentException(sprintf('Invalid user-path URI passed (%s), user-path URIs must use the user-path:// scheme', $parts['scheme'])); + } + return ltrim($uri, 'user-path://'); + } + } diff --git a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php index a846a6e..8584d8d 100644 --- a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; @@ -243,7 +244,7 @@ public function viewElements(FieldItemListInterface $items) { protected function buildUrl(LinkItemInterface $item) { $uri = $item->uri; if (parse_url($uri, PHP_URL_SCHEME) === 'user-path') { - $uri = substr($uri, 12); + $uri = UrlHelper::userPathFromUri($uri); } // @todo Consider updating the usage of the path validator with whatever diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 8ab89fb..5f41270 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -8,6 +8,7 @@ namespace Drupal\link\Plugin\Field\FieldType; use Drupal\Component\Utility\Random; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; @@ -154,7 +155,7 @@ public function isExternal() { // External links don't resolve to a route. $uri = $this->uri; if (parse_url($uri, PHP_URL_SCHEME) === 'user-path') { - $uri = substr($uri, 12); + $uri = UrlHelper::userPathFromUri($uri); } $url = \Drupal::pathValidator()->getUrlIfValid($uri); return $url->isExternal(); @@ -175,7 +176,7 @@ public static function mainPropertyName() { public function getUrl() { $uri = $this->uri; if (parse_url($uri, PHP_URL_SCHEME) === 'user-path') { - $uri = substr($uri, 12); + $uri = UrlHelper::userPathFromUri($uri); } return \Drupal::pathValidator()->getUrlIfValidWithoutAccessCheck($uri); } diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index a055a3b..33d1a56 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -8,6 +8,7 @@ namespace Drupal\link\Plugin\Field\FieldWidget; use Drupal\Component\Utility\Unicode; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; @@ -45,7 +46,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen if (isset($items[$delta]->uri)) { $uri = $items[$delta]->uri; if (parse_url($uri, PHP_URL_SCHEME) === 'user-path') { - $uri = substr($uri, 12); + $uri = UrlHelper::userPathFromUri($uri); } if ($url = \Drupal::pathValidator()->getUrlIfValid($uri)) { $url->setOptions($items[$delta]->options ?: []); diff --git a/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php b/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php index d448add..f4d73b1 100644 --- a/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php +++ b/core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php @@ -7,6 +7,7 @@ namespace Drupal\link\Plugin\Validation\Constraint; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Url; use Drupal\link\LinkItemInterface; use Symfony\Component\Validator\Constraint; @@ -56,7 +57,7 @@ public function validate($value, Constraint $constraint) { $url_string = $link_item->uri; $scheme = parse_url($url_string, PHP_URL_SCHEME); if ($scheme === 'user-path') { - $url_string = substr($url_string, 12); + $url_string = UrlHelper::userPathFromUri($url_string); } // Validate the url property. if (($scheme === 'user-path' || $scheme === 'base') && !($link_type & LinkItemInterface::LINK_INTERNAL)) {