Score:0

Using Twig in global custom text to check for a specific content type

in flag

I am filtering for two content types to display in a view: Artwork Page and Cover Page. I want to change the HTML markup for Cover Page.

I have researched and tried many field names for Content Type, but with none of them I get what I am trying to achieve. In this instance, the replacement pattern for Content Type is {{ type }}; using it doesn't work. I've tried both the machine name (cover_page) and the name (Cover Page), but neither of them worked.

{% if type == 'cover_page' %}
  <h1>THIS IS THE BEGINNING</h1>
{% endif %}

{% if type == 'Cover Page' %}
  <h1>THIS IS THE BEGINNING</h1>
{% endif %}
sonfd avatar
in flag
I can't tell if you're trying to use this token in a custom text within a views area, e.g. header or footer, or as one of the fields in the row display. _I don't believe_ you'll be able to use a field's token value in a header / footer custom text as it won't have the proper context to generate a value (the value could be different for each result in your view).
Score:0
cn flag

The content type field is a reference field and contains the machine name of the content type as target id.

Configure the field formatter Entity ID:

Configure field: Content: Content type

Formatter
Entity ID ▼

Or use the property target_id when rewriting the field itself:

{{ type__target_id }}

You can add a second instance of the field and hide it if you only want to use the machine name for logic and don't want to display it.

in flag
Thank you 4k4. The Formatter label did it! All I needed was the machine name for the twig test.
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.