Problem/Motivation
Starting from Drupal 8, we started to use paths with a leading slash in Drupal, so something like this:
$url = \Drupal\Core\Url::fromUserInput('some/internal-path');
will produce an exception:InvalidArgumentException: The user-entered string 'some/internal-path' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (line 216 of core/lib/Drupal/Core/Url.php).
And if we add a leading slash, it starts to work well:
$url = \Drupal\Core\Url::fromUserInput('/some/internal-path');
// Will compose the "base:some/internal-path" uri.
But from JS side we suddenly expect the path to not start with slash, because the same call will produce a path with two slashes:
console.log(Drupal.url('/some/internal-path'));
// Outputs: `//some/internal-path`.
// Or with non-default language and non-empty base_path - something like `/web/de//some/internal-path`.
Steps to reproduce
1. On the frontend - get an internal Drupal path from the backeng, eg from the drupalSettings.
2. Try to build a relative URL with this path, by adding the base path and the language prefix.
You will get the wrong composed path.
Proposed resolution
To not break the legacy behavior, I'd suggest leaving the current function unchanged, and creating a new function Drupal.urlFromPath(path)
.
What do you think about this idea?