By Andrea

2016-04-26 11:23:32 8 Comments

I have a Link field named field_my_link (machine name).

Within a Twig template I can get the link's URL value with this:

{{ node.field_my_link.uri }}

If inside the Link's URL there is an external URL, e.g. it works good.

But, if inside the Link's URL there is stored an internal URL, e.g. "/node/11", I get a value like this:


with the "internal:" prefix.

How can I get the valid URL?

For example, if the link's URL is "" I want "" (this already works), if the link's URL is "/node/11" I want the relative URL "/node/11", if the link's URL is "<front>" I want the URL "/", etc.


I can get the value I want within a PHP preprocess function, using this:

$url = $variables['node']->get('field_my_link')->first()->getUrl();

How can I access the same value within the Twig template?

I know I can do this within the preprocess function:

$variables['my_url'] = $url;

Then access the url within Twig using {{ my_url }}, but I would avoid to write a preprocess function each time I have a link field.


@AnTSaSk 2018-11-27 10:21:30

I know it's an old post but with the last version of Drupal 8 (8.6) you can directly display the url for both version (external and internal):


And if you need to know if the link is external or not (for my case it was for the target), you can use that:



<a href="{{ node.field_link.0.url }}" title="Title" target="{{ node.field_link.0.url.external ? 'target="_blank"' : 'target="_self"' }}"

@leymannx 2017-10-11 13:59:33

You have to cover both cases: external and internal URLs.

First let's check if the URL is external and simply print its *.uri.

Else if it's internal we have to wire its route name and parameters through Drupal's path($name, $parameters, $options) function.

{% if node.field_link.0.url.external %}
  <a href="{{ node.field_link.uri }}">
    {{ node.field_link.title }}
{% else %}
  <a href="{{ path(node.field_link.0.url.routeName, node.field_link.0.url.routeParameters) }}">
    {{ node.field_link.title }}
{% endif %}

@user33560 2018-03-01 08:56:15

Thank you, this is exactly what I needed: get the url from a link field as a string: {% set url = path(node.field_link.0.url.routeName, node.field_link.0.url.routeParameters) %} Then I can create a custom link:` <a href="{{ url }}" class="mycustomclass">custom link text</a> No preprocess needed and no need to go via content.field_link, so no need to do anything in the 'Manage display' for the link field just to get the url.

@cbovard 2017-05-01 21:38:06

I had to do this on a Link Field in a Block. This worked well:

{{ content.field_my_link[0]['#url']|render }}

@eric.chenchao 2017-06-06 02:35:05

It is also fine to use {{ content.field_my_link[0]['#url'] }} without render, as in twig template, render array are automatically rendered when printed.

@4k4 2016-04-26 11:39:26

Your PHP from your latest edit would translate into twig like this:

{{ node.field_my_link.0.url }}

This works like this, 0 returns the first item of the field item list, url gets the url object and because twig will cast this object as a string this will call the magic method toString() and will output the url as a string value.

You only need to use url, because twig looks automatically for the method getxyz() if there is no property with that name xyz.

@Andrea 2016-04-26 12:25:04

Thank you. Now I make a test with this function. Do you know if can I use this function also on links' URIs? What happen if node.field_my_link.uri is an external url like

@Andrea 2016-04-26 12:32:17

I tried with file_url(node.field_my_link.uri), and if node.field_my_link.uri is "" it works (it leaves the url unchanged), but if node.field_my_link.uri is "internal:/" the function returns "/internal%3A/" (instead of "/")..

@Andrea 2016-04-30 07:32:43

I just saw your edit.. Sadly it doesn't works, the code node.field_my_link.entity.uri.value returns a null value.. Any other ideas?

@4k4 2016-04-30 11:27:54

@Andrea, I edited my answer.

@Andrea 2016-04-30 11:53:07

Yeah, it works. The syntax is a bit scary, but it does its job.

@mogio 2016-12-06 13:30:42

This shouldn't be marked as "solution" as the file_url() function works just with files and not with link fields. The thread starter asked for the raw url output of a "link field".

@NikLP 2017-03-24 12:27:06

I don't believe this approach works with {% if content.field.0.url %} because there's no implicit 'request to cast to string'. Is there a way to cast the variable to get the desired output for string comparison?

@aaronbauman 2017-03-29 19:53:35

This doesn't work for me. field_link.0.url throws a syntax error. I had to use field_link[0].url instead.

@Nate Lampton 2019-02-19 01:17:37

To answer NikLP above, if you need the string in a conditional, you can specifically call the toString method to get a string such as {% if content.field.0.url.toString %}

@Tim 2016-10-22 06:02:55

You can access the render array element directly, in your case:

{{ node.field_my_link[0]['#url'] }}

@esQmo_ 2018-10-23 21:05:39

This one worked for my.

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] How to get the value / URL of a Link field in a Twig template?

3 Answered Questions

[SOLVED] Entity print get variable in twig template

1 Answered Questions

[SOLVED] Get node internal path from path alias in Twig template

4 Answered Questions

[SOLVED] Customize the HTML of a link field from a custom theme

1 Answered Questions

[SOLVED] Get path/url/uri of Entity reference field to content in a Twig template?

  • 2018-01-29 09:37:52
  • zazie
  • 2681 View
  • -1 Score
  • 1 Answer
  • Tags:   entities uri

3 Answered Questions

[SOLVED] How do I get the node URL in a Twig template?

  • 2016-05-30 12:12:50
  • inalto
  • 24758 View
  • 19 Score
  • 3 Answer
  • Tags:   nodes uri

2 Answered Questions

[SOLVED] How to get media entity field value in twig?

  • 2017-04-20 10:25:23
  • Philipp Michael
  • 2026 View
  • 3 Score
  • 2 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] How to access the node title within a field template?

  • 2016-06-09 08:13:52
  • Alex
  • 3382 View
  • 1 Score
  • 2 Answer
  • Tags:   8

1 Answered Questions

[SOLVED] How to retrieve image URL from Media Bundle field in Twig Template

  • 2016-05-17 07:33:12
  • littledynamo
  • 2334 View
  • 1 Score
  • 1 Answer
  • Tags:   theming 8 media

0 Answered Questions

I want to use an argument from the path in a twig template (as a html class)

  • 2016-02-01 08:18:13
  • RominRonin
  • 556 View
  • 1 Score
  • 0 Answer
  • Tags:   8 uri theming

Sponsored Content