Is there a way to fetch or print the node url/address from a entity reference to my twig template.

I have already tried {{ related_links.entity.uri }}, {{ related_links.entity.url }} or {{ related_links.entity.path }} but nothing seems to work


{% for related_links in node.field_pbn_additional_items %}

    <h2>{{ related_links.entity.label }}</h2>

{% endfor %}


enter image description here


1 Answer 1


No need to add the <a> tags, because Drupal will automatically create the link.

So just simply do <h2>{{ related_links.entity.toLink }}</h2>

You don't even need the label with .toLink() because Drupal will automatically grab it when generating the link.

The above is suppose to work, but I get an error

Twig_Sandbox_SecurityError: Calling "toUrl" method on a "Drupal\node\Entity\Node" object is not allowed. in Drupal\Core\Template\TwigSandboxPolicy->checkMethodAllowed() (line 105 of /home/ubuntu/workspace/drupal-8.5.3/core/themes/bartik/templates/node.html.twig).

There is an issue about this on drupal.org Add $entity->toUrl() and $entity->toLink() methods to whitelist in Twig sandbox policy applying the patch in #30 solved the issue.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.