I am trying to get the node URL in a Twig template. {{ content.node_link }}
returns the full link, but I only need the URL.
How do I retrieve the node URL?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityAccording to Functions - In Twig Templates you can use is simple by
{{ path('entity.node.canonical', {'node': node.id}) }}
node--
based twig template will give you the same result.
– Eric Steinborn
Aug 29 '17 at 13:20
In a node.html.twig you can directly use {{ url }}
<a href="{{ url }}">read more</a>
For more information read comment in core template : /core/modules/node/templates/node.html.twig
:
{#
/**
* @file
* Default theme implementation to display a node.
*
* Available variables:
* - node: The node entity with limited access to object properties and methods.
* ...
* - url: Direct URL of the current node.
* ...
#}
...
<article{{ attributes }}>
{{ title_prefix }}
{% if not page %}
<h2{{ title_attributes }}>
<a href="{{ url }}" rel="bookmark">{{ label }}</a>
</h2>
{% endif %}
{{ title_suffix }}
I have found this solution that works, but is quite convoluted.
{{ path('entity.node.canonical', {'node': content['#node'].id() }) }}
content['#node'].id()
is correct for the DS context.
– masterchief
Jul 31 '18 at 0:58
url
is an available variable. Thus simply printing{{ url }}
for example will render the raw URL,/node/[nid]
i.e./node/30
without any wrapping markup. Is that what you are looking to do? – Danny Englander May 30 '16 at 16:32