Score:0

Twig: display linkfield with optional title

pa flag
ase

I created a linkfield within a paragraph with an optional title. The textlink should only be displayed, when the title is not empty. I need the URL from the linkfield for another field (an image where the users can click to open the same URL from the linkfield), so the editor can chose if he wants the textlink as well to be displayed or only the linked image.

{% if content.field_link.0['#title']|render %}
<a href="{{ content.field_link.0['#url'] }}" target="{{ content.field_link.0['#options']['attributes']['target'] }}">{{ content.field_link.0['#title'] }}</a>
{% endif %}

Unfortunately the textlink will be displayed anyway, even if the title of the textlink is empty :-( If the title of the textlink is empty, the URL of the link will be displayed (linked).

What am I doing wrong? Is there another solution for this? Thanks

Score:0
cn flag

You are using a render filter in your condition. When the title is empty, the render filter will still return the link URL because that's part of what's being rendered.

This is why your link is displayed even when the title is empty.

Instead of using the render filter, you can check directly if the title is not empty using the empty test:

{% if content.field_link.0 is defined and content.field_link.0['#title'] is not empty %}
  <a href="{{ content.field_link.0['#url'] }}" target="{{ content.field_link.0['#options']['attributes']['target'] }}">{{ content.field_link.0['#title'] }}</a>
{% endif %}

The empty test will return true if the variable is empty or not set and false otherwise. By using the not operator, you can check if the title is not empty.

ase avatar
pa flag
ase
Thank you for your reply. Unfortunately the code is not working, the URL ist still visible even if the optional title field is empty.
Ziftman avatar
cn flag
I update the snippet , check it again please
ase avatar
pa flag
ase
Thanks again for your support, it is highly appreciated.
Score:0
ru flag

For doing logic I generally recommened the raw entity object variable (paragraph in a paragraph template, node in a node template, etc) instead of the content HTML render array.

{% if paragraph.field_link.0.title %}
  ...your stuff here...
{% endif %}

The schema is entity_type.field_name.numeric_index.database_column
(database_column is usually value, except link fields have a uri and a title; and entity references use target_id instead of value)

ase avatar
pa flag
ase
Yeah! Thank you very much. This helped a lot, you are the best!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.