How to get the valid URL of a Link field from within a Twig template?


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

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

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.

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 %}
Tags: Theming / Drupal 8 / Uri

Similar questions

One-time link without valid email address Spam Registrations
I have a D6 site that gets a lot of SPAM account registration and they all follow a pattern, however from different IP addresses (often during the following process): 1) User account is created 2) One Time registration link is used to log in 3) account page is visited 4) node/add page is visited So, how are the bots logging into my site? How can th...
Check if there is already a valid one-time-password -reset link
I am using drupal 7. I have this functionality that when a user clicks the reset password he gets a one time link on which if he/she clicks then they gets to some page where they can change their password. But this functionality is somewhat need to be modified. Suppose if they don't click on one link sent to them and then again request forget passw...
How to use 2 twig template for for one field (or how to add a variable to a template from a template)
Let say I have a node template which is calling twice the same field like the following: I also have a preprocessing function which is adding VAR1 and VAR2 to the 'my_field' array like the following: I would like that the first call of 'My_field' use the default twig template and the 'VAR1' value... As you can imagine, I could do it as this is the ...
Force redirect if a valid taxonomy term is not at the start of the URL?
As a follow on from this question I am trying to prepend all URLs on the site with a taxonomy term. The term itself is handled by a custom module for the most part, however elements of the site such as the calendar modules page view don't allow for me to intervene and theme the link accordingly. If a user goes to the following URL: Then they should...
Need full Taxonomy URL to be valid else redirect 404
I have two taxonomy term pages with the following details: When I am typing url then content related to term id 2 is getting loaded which is correct But the problem is when I am typing which is not a valid url then the content of Term id 1 is getting loaded since it is matchin...
Accessing Content: Path field within views_view_unformatted TWIG template
I have a view that has the fields: Content: ID, Content: Title and Content: Path. In my views-view-module.html.twig file I can access the fields via the following: But I cannot seem to retrieve the path field using: Do I do this the same way or am I missing a trick? I've tried to use dump/kint but I keep getting the WSOD. Can anyone offer me a solu...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.