How to deal with 'internal:' in your link->uri? I have a link field, which I can populate with both internal and external links:


  /node/123


I'm building an API that parses the node and returns the URL from the link field:

$node-&gt;my_field-&gt;uri

Which returns internal:/node/123 which makes this URL useless.

If I try using use Drupal\Core\Url; and Url::fromUri($node-&gt;my_field-&gt;uri), I get a 500 error!

So what is the correct approach for getting a formatted URL, that can parse both internal and external links?
just kidding. To help future devs with this issue, check the logs *duh and you'll see this:


  PHP message: Uncaught PHP Exception InvalidArgumentException: "The URI '' is invalid.


So, check for the uri value first!

$node-&gt;my_field-&gt;uri ? Url::fromUri($node-&gt;my_field-&gt;uri)-&gt;toString() : null,