How to process url from user input? I am creating LinkedImage field, formatter and widget so that images can be linked to somewhere.

And I have an issue with the input. The user can set internal or external links like /node/1, &lt;front&gt; or http://www.google.com. But the Url object that is required for link render element has no method that would work with all of these.

The Url::fromUserInput will work with internal paths but not with &lt;front&gt;. The Url::fromUri() will work with http://www.google.com but not with &lt;front&gt; or /node/1.

So I had to implement logic to distinguish the format and also I had to add validation handler into the widget telling the user that the link has to start with forward slash or with http prefix.

But I do not like this approach. Seems like this is not how it is supposed to be done.

So how can I properly construct Url object from these three types of input?

Also another issue here is that if "malformed" URL is already set (ie. it hasn't been validated via my UI validation handler) then I will get WSOD and that is just really bad UX.

Plus, as you can see, &lt;front&gt; is not working with any of these methods whic hrequires additional logic.
I had the same problem, so please try this:

$url_object = \Drupal::service('path.validator')-&gt;getUrlIfValid($path);
$url_object-&gt;setAbsolute();
$url = $url_object-&gt;toString();