Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Get the URL/Path of a Referenced Entity in Node Template - Drupal 8

When using an entity reference field on a node, such as to reference another node, it might be necessary to get the raw path/URL of the referenced entity.

Drupal allows you to render a label via the Managed Display options for your content type, but it doesn't offer the ability to render just the raw link.

If you have complicated theming going on in your node template, this might not be workable in your situation.

For cases where you need to retrieve the raw path, link, URL, URI (or similar), here's the solution you should use:

{{ path('entity.node.canonical', {'node':  node.field_entity_reference_field.entity.id}) }}

Paste that in your node.html.twig file and you'll be good to go!

Don't forget to switch out the bold text in the syntax for the machine name of your entity reference field. If your field name is 'field_example', your syntax will look like this:

{{ path('entity.node.canonical', {'node':  node.field_example.entity.id}) }}

Be sure to clear your Drupal cache after making any changes to your node.html.twig file.

Happy Drupal theming!