How to get the raw value of the url in a link field I'm doing this:

$link = $node-&gt;field_link-&gt;first();
if ($link) {
  $url = $link-&gt;getUrl()-&gt;toString();
  ...
}


If the value in the link field is /node/123 but there is a URL alias defined as:

Alias: /about-us    System path: /node/123


I get /about-us in $url. I want to get /node/123.

How do I get the raw url value instead of the aliased value?
Instead of rendering the URL object with toString() use the method Url::getInternalPath:

$url = $link-&gt;getUrl();
if ($url-&gt;isRouted()) {
  $path = $url-&gt;getInternalPath();
}