0

I am using a template which has an 'if' argument to alter the output of a div containing an image field. I need to add my own condition (within comments) to it. Currently the output is creating the image twice (see attached screenshot).

<!-- added for publication image formatting -->
  {% if content.field_download_resource|render %}
  <div{{ attributes.addClass('pub-img') }}> {{ content.field_image }}
  {% endif %}
  </div>
<!-- end add for publication image formatting -->

<div class="post-thumbnail post-{{ post_format }}">
  {% if post_format == 'video' or post_format == 'audio' and gva_iframe %}
    {{ gva_iframe|raw }}
  {% elseif post_format == 'gallery' and content.field_post_gallery %}
    {{ content.field_post_gallery }}
  {% else %}
    {{ content.field_image }}
  {% endif %}
</div>

The first 'if' argument confirms if there is a value for the field download_resource, and if there is a value, it adds the class '.pub-img' to the div containing the output of field_image. The second 'if' argument acts on the same div but displays the content of different fields, OR the same field_image with a different class applied to it, depending on different conditions. Right now my page output is displaying BOTH images, as below:

enter image description here

I don't want that to happen. What I need to do is ONLY display the output of the first 'if' argument in the div (and remove the display of the larger image) if there is a value for the field_download_resource, OR ELSE display ONLY the larger image and not the smaller image (output of the first 'if' argument).

I have tried various methods to combine the two 'if' arguments into one, but nothing seems to work. I get the "unexpected error" message. I have reached the limit of my knowledge of drupl 8 Twig theming, and am now appealing to the experts to lend a hand.

0

So here's the correct code, which I figured out after a lot of trial and error. Posting it here so that it helps someone else in the same predicament.

<!-- added for publication image formatting -->
  {% if content.field_download_resource|render %}
  <div class="pub-img"> {{ content.field_image }} </div>
<!-- end add for publication image formatting -->

{% elseif content.field_download_resource|render is empty %}

<div class="post-thumbnail post-{{ post_format }}">
  {% if post_format == 'video' or post_format == 'audio' and gva_iframe %}
    {{ gva_iframe|raw }}
  {% elseif post_format == 'gallery' and content.field_post_gallery %}
    {{ content.field_post_gallery }}
  {% else %}
    {{ content.field_image }}
  {% endif %}
</div>

{% endif %}

Thanks...

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.