I stepped over the module when looking for a way to render TWIG conditionals in content.

I installed the module together with codemoirror editor. Created a field and added some text like

some static text
{% if field1 %}
{{ field2 }}
{{ field3 }}
some additional static text
{% endif %}

Rendering the node gives no output, something's missing.

Comments

bkraegelin created an issue. See original summary.

chi’s picture

Rendering the node gives no output, something's missing.

Do you mean it does not even render a static text? Can you check the field is enabled on "Manage display" tab?

bkraegelin’s picture

I just did a reinstall of twig_field and codemirror_editor.

  • I added field TwigTest to my taxonomy of type Twig template.
  • It is enabled in Form Display using Template editor.
  • It is enabled in Manage Display as Rendered Twig template.
  • I added a new term and filled in some text. All fields get rendered, TwigTest is missing.

Is there anthing missing? Perhaps activating StringLoader?

chi’s picture

I've just followed your steps and it worked for me as expected. This needs more details to reproduce.

bkraegelin’s picture

How can I dig deeper in testing?

I installed devel module, but I'm not familiar with it. My Drupal experience is back to 5.x and 6.x :(

chi’s picture

Can you try this on fresh Drupal installation with default theme?

bkraegelin’s picture

Yes, it works. Thanks. Maybe a damaged Drupal instance.

How can I access other fields of my taxonomy term from within this Twig field?

chi’s picture

How can I access other fields of my taxonomy term from within this Twig field?

In field settings you should specify display mode to fetch context from.

bkraegelin’s picture

Thanks, it works.

Last question:
I want to create a taxonomy to define "text blocks" for GPDR. These text blocks should have placeholders (Twig variables and conditionals). For a particular server I want to assign the different text blocks using my custom taxonomy.

From within the text blocks (taxonomy term field) I want to access fields of the server node to fill in placeholders based on conditionals "{% if field %}". Do you have an idea for such an application?

chi’s picture

I don't quite understand your task. Note that checking field values can be tricky because fields in Twig template are actually render arrays.
See #2547559: Checking for empty fields in Twig templates is complicated for details. Instead of fields use properties of entity object (node or taxonomy_term) in condtions.

chi’s picture

Status: Active » Fixed

Seems resolved.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.