I have an entity reference field set in Manage Display to 'Rendered Entity' and am working with the field's template to conditionally show the link based on the referenced node's published status. I'm using 'Rendered Entity' so I have access to isPublished().

How can I get just the path of the referenced node in the field's template?

Within {% for item in items %}, this works great to get the referenced node's title: {{ item.content['#node'].title.value }}, but I can't find what I need for the url.

  • There is already a "Label" field formatter for entity reference fields that allows you to render the title of the referenced entity either as plain text or as a link to the referenced entity. See web/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php for the implementation of this field formatter. Should you want to customize the output, I suggest you extend this formatter and override the viewElements() method.
    – Joseph
    Apr 6 at 16:32


Your Answer

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

Browse other questions tagged or ask your own question.