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
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.
Url::fromUserInput will work with internal paths but not with
Url::fromUri() will work with
http://www.google.com but not with
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,
<front> is not working with any of these methods whic hrequires additional logic.