0

I have a specific content type that I am overwriting the template node--contenttype.html.twig.

This conent type has 4 fields.

  • Title
  • Body
  • Image
  • Video

I am trying to hide the image field from displaying if VIDEO field has value. Which I am doing by using this code in custom twig template.

{% if node.field_video is not empty %}
{{ content.field_video }}
{% else %}
  {{ content.field_image }}
{% endif %}

The only problem is that I get one of the fields printed as needed plus I get both fields shown.

Video filed is displayed, plus I get an image and video shown again. Wherever those fields are positioned in MANAGE DISPLAY.

If I disable the two fields in MANAGE DISPLAY then, nothing shows up at all.

How can this be fixed?

  • You can’t disable the fields from display - they will not be added to the content render array. – Kevin Aug 22 '18 at 22:07
3

You wrote that you are using Twig conditions to selectively print fields, but they show up twice now. This most likely is, because your template still renders the entire content variable somewhere like {{ content }}.

You can easily avoid the fields being rendered twice by using the without Twig filter. Locate the {{ content }} part in your template and alter it to

{{ content|without('field_video', 'field_image') }}
  • Mario, do you have any tips on how to implement this on a display suite template? I have created a custom layout and placed it under the templates directory in my subtheme. But on the ds-custom-lyout.html.twig I only see regisons, no contect variable. {% set left = left|render %} {% set middle = middle|render %} {% set right = right|render %} {% if (left and not right) or (right and not left) %} {% set layout_class = 'group-one-sidebar' %} {% elseif (left and right) %} {% set layout_class = 'group-two-sidebars' %} {% elseif (left) %} – Progonat Oct 3 '18 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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