0

My site theme includes customised templates for 4 regions in the footer, all containing identical code:

  set classes = [
    'region',
    'region-' ~ region|clean_class,
    'col-sm-12',
    'col-md-6',
    'col-lg-3',
  ]
%}
{% if content %}
  <section{{ attributes.addClass(classes) }}>
    {{ content }}
  </section>
{% endif %}

However, the HTML it produces varies.

In the first two regions, the class row is being inserted - despite it not appearing in the classes array:

<!-- BEGIN OUTPUT from 'themes/custom/my_theme/templates/layout/region--footer-first.html.twig' -->
  <section class="row region region-footer-first col-sm-12 col-md-6 col-lg-3">

In the last 2 regions, the row class is missing - which is the correct code I want:

<!-- BEGIN OUTPUT from 'themes/custom/my_theme/templates/layout/region--footer-third.html.twig' -->
  <section class="region region-footer-first col-sm-12 col-md-6 col-lg-3">

I can't figure out where it's coming from, so my workaround is to include an additional step that removes the class, in the first two region templates only:

<section{{ attributes.addClass(classes).removeClass('row') }}>

This gives the result I want, but I'm still not sure where the row class comes from and why it was being applied inconsistently??

1 Answer 1

2

Those 5 classes are being added using .addClass() to a pre-existing {{ attributes }} Twig object. The 'row' class must already be in the attributes object when Drupal passes it to the Twig template.

Typically, the place where something like that would happen is in your .theme file (or the parent theme's .theme file if yours is a subtheme). Look for a function like THEMENAME_preprocess_region() and see if you can find a hard-coded "row" string being thrown in there.

Alternatively, some fancier theme systems might expose configuration options in the Drupal interface that could be setting that class. You'd find those under Admin > Appearance, then in "Settings" for your active theme.

1
  • Ah, that was the culprit! My theme settings in the UI had the 'row' class being added on the first 2 footer regions.
    – Timmah
    Commented Mar 25, 2021 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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