What is the correct way to get the link url? We have a Basic Page content type with a link field (field_link). When we call the mymodule_preprocess_node(&amp;$variables) hook in a custom module we use ksm($node-&gt;get('field_link')-&gt;first()) and it returns a Drupal\link\Plugin\Field\FieldType\LinkItem. We need to extract the URL from this fields and have found two ways to do it (see below). What is the correct way to get the URL from the field?

The reason for extracting the URL is that we need to wrap a set of custom fields with the link so we can have a HTML structure similar to below. 

HTML structure:

&lt;a href="/some/path" target="_blank"&gt;
  &lt;img src="some/image/path"&gt;
  &lt;span&gt; Some title field &lt;/span&gt;
&lt;/a&gt;


mymodule.module

...
function mymodule_preprocess_node(&amp;$variables) {
  $node = $variables['node'];
  $node_field_link = $node-&gt;get('field_link')-&gt;first();

  // Do we use this?
  $node_field_link_url = $node_field_link-&gt;getUrl()-&gt;getUri();

  // Or do we use?
  $node_field_link_url = $node_field_link-&gt;get('uri')-&gt;getValue();
}
...
I find the best way to get a url especially when I'm using it in cases where I need to wrap custom html around it is to get the url object and then use the toString Method.

$node_field_link_url = $node_field_link-&gt;getUrl()-&gt;toString();


The getUrl() method of the LinkItem object returns a Url object. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x 
And Url objects have a toString method that returns the string representation of that url.