2

I have a Entity reference field which links to internal content/node.

I can get the title with {{ node.field_related_project_internal.entity.label }}

But how can I get the raw path/url/uri of the linked content/node? The soloutions for a Link Field to not work here: How to get the valid URL of a Link field from within a Twig template?

2
  • {{ node.field_related_project_internal.entity.0.url }} not working? Can u do {{ dump(node.field_related_project_internal.entity) }} & check u r getting ur field or not?
    – Mike
    Commented Jan 29, 2018 at 9:40
  • {{ node.field_related_project_internal.entity.0.url }} is not working / no output. {{ dump(node.field_related_project_internal.entity) }} gives me a blank page.
    – zazie
    Commented Jan 29, 2018 at 9:52

1 Answer 1

10

The url is not an entity field, so you can't access it directly. Also the entity methods to generate url/links are not accessible in Twig because of the sandbox policies. You can build a path though, if you have the id of the referenced node:

{% if not node.field_ref.isempty %}
  {{ path('entity.node.canonical', {'node':  node.field_ref.entity.id}) }}
{% endif %}

Reference: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates

1
  • It works and I understand now. Thank you for the explanation!
    – zazie
    Commented Jan 29, 2018 at 10:42

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.