Get Absolute url using hook_preprocess_node We are attempting to change the $variables['url'] from a relative to absolute URL within the mymodule_preprocess_node() hook. The $variables['url'] is displayed as a &lt;a&gt; element within our node--page.html.twig template. 

How can we set the $variable['url'] so that it uses the absolute URL?

node--page.html.twig

&lt;a href="{{ url }}"&gt;
  {{ label }}
  {{ content.field_text }} {# custom text field #}
&lt;/a&gt;


What we tried within mymodule_preprocess_node().

use Drupal\Core\Url; 

function mymodule_preprocess_node($variables) {
  $options = [
    'absolute' =&gt; true,
  ];
  $variables['url'] = URL::fromUri($variables['url'], $options);
}


This results in the following error message:


  The URI &#039;&#039; is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 281 of core\lib\Drupal\Core\Url.php).
In order to get this to work, I did the following. This assume you have access to the $node object, which you would if you are performing this within the hook_preprocess_node() hook. 

$variables['url'] = $node-&gt;toUrl()-&gt;setAbsolute()-&gt;toString();