Skip to main content

Tales of… lean code problems

Drupal Snippets

The case of the missing YouTube background thumbnail in the Klaro privacy layer

This problem will only occur if you write your own themes and strive to have lean HTML code.

The problem: If you are working with the Klaro Cookie & Consent Management module in Drupal the YouTube privacy layer should show the thumbnail of the video in the background. In my theme it didn’t.

So, on this site the YouTube Consent Layer looked like this:

Klaro privacy layer with text and a grey background

I thought this was how it was supposed to look. Until someone showed me the layer on another site. My example should have looked like this by default:

Klaro privacy layer with text and a thumbnail in the background

If you want to know more about this module watch: Drupal CMS Golden standard for privacy and data protection

After some back and forth in my theme I found the culprit: I had overwritten field.html.twig.

Drupal’s Core uses this Twig code for field.html.twig:

{%
 set title_classes = [
   label_display == 'visually_hidden' ? 'visually-hidden',
 ]
%}
{% if label_hidden %}
 {% if multiple %}
   <div {{ attributes }}>
     {% for item in items %}
       <div {{ item.attributes }}> {{ item.content }} </div>
     {% endfor %}
   </div>
 {% else %}
   {% for item in items %}
     <div {{ attributes }}> {{ item.content }} </div>
   {% endfor %}
 {% endif %}
{% else %}
 <div {{ attributes }}>
   <div {{ title_attributes. addClass(title_classes) }}> {{ label }} </div>
   {% if multiple %}
     <div>
   {% endif %}
   {% for item in items %}
     <div {{ item.attributes }}> {{ item.content }} </div>
   {% endfor %}
   {% if multiple %}
     </div>
   {% endif %}
 </div>
{% endif %}

This means Drupal wraps every field in an (at least one) extra div – the main ingredient in what we used to call a div soup: dozens and hundreds of unnecessary div containers.

So one on the very first things I do in every theme is to get rid of this nonsense. I just use:

{% for item in items %}
 {{ item.content }}
{% endfor %}

Much simpler. And yes, sometimes I need extra markup for a field. But even in bigger projects I usually only need two or three specific field templates to do this. For 99% of fields my minimal template is fine.

But now, nearly ten years after Drupal 8 was released, I had the first case where my minimal template conflicted with a feature from a module. Klaro actually needs the wrapping div for embedded videos.

So, if I want Klaro to show background thumbnails, I have to add a template for field--media--field-media-oembed-video.html.twig with the following code (this is the most minimal code I found that Klaro is ok with.)

{% for item in items %}
<div class="field">
  {{ item.content }}
</div>
{% endfor %}

And voila, the background images are showing up. Together with the title of the YouTube video.

06.06.2025 × Drupal × Twig

Now, why would I want to change field.html.twig in the first place? My main aspect is lean code: If a div has no function there is no reason for that div to be there. There are other aspects to this. I think that deserves its own Drupal Snippet next week.

Write a comment

House rules: Comments are moderated. The E-Mail will not be shown publicly, but in rare cases I might respond to a comment via mail. If your website points to a shop or shady website, I will not show the link.

The content of this field is kept private and will not be shown publicly.

Curiosity Clicker:
The message in the bottle
was clicked 0 times.