0

I have a set of nodes, some of which are just a title, while others have body and other content.

I'm trying to create a view, which lists the node titles. If the body of the node contains content, the title should link to the node, but if the node body is empty, it should just display the title as plain text.

I think I can achieve this by rewriting the title field in the view with TWIG.

I've got it working so it detects the body and displays a link, but I'm not sure what to put for "url" in the link.

{% if body|render|trim is not empty %}
<a href="{{ url }}">{{ title }}</a>
{% else %}
{{ title }}
{% endif %}

How can the TWIG code get the url of the node to link to?

1 Answer 1

3

In the view fields search for "Link to content", add the field, exclude this from presentation and move it to the top, and now you can use:

{% if body|render|trim is not empty %}
  {{ view_node }}
{% else %}
  {{ title }}
{% endif %}
1
  • Brilliant. I'll give that a try soon. Thanks for the quick answer! Nov 11, 2019 at 8:47

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.

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