3

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

<div>

{% for related_links in node.field_pbn_additional_items %}

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

{% endfor %}

</div>

enter image description here

6
  • Rather than playing the guessing game, how about debugging it? Get good, A: What is easiest way to debug variables in nodes, views, blocks and page twig templates?
    – No Sssweat
    Commented Jun 22, 2018 at 4:31
  • @NoSssweat already tried using {{ kint() }} to debug the issue but I just can't seem to find what I'm looking for
    – Xavier
    Commented Jun 22, 2018 at 9:25
  • did you check available methods?
    – No Sssweat
    Commented Jun 22, 2018 at 9:28
  • @NoSssweat is it under Node ? Sorry for my ignorance
    – Xavier
    Commented Jun 23, 2018 at 1:57
  • You didn't read the answer I provided in the link above, didn't ya? 😜 Anyhow, it's here
    – No Sssweat
    Commented Jun 23, 2018 at 3:33

1 Answer 1

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.

2

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.