Normal view

Received — 6 October 2025 Drupal - Open Source Content Platform | #Celebr8D8

How to reliably add Bootstrap classes to blocks in a specific region in Drupal 11?

Hi everyone,

I’m working on a custom Drupal 11 subtheme based on Bootstrap 5 and I want to achieve the following:

Goal:
I have a footer region called footerblocks and I want all blocks placed in this region to automatically get Bootstrap 5 classes like col-md-4 and mb-4, so they layout nicely in 3 columns.

What I’ve tried:

  1. Using hook_preprocess_region()Problem:foreach (Element::children($variables['content']) as $key) { $variables['content'][$key]['#is_in_footerblocks'] = TRUE; }
    • I iterate through $variables['content'] with Element::children() to flag blocks inside footerblocks.
    • Then, in hook_preprocess_block(), I check for this flag and add the classes.
    • Sometimes $variables['content'] is a Markup object instead of an array, which causes a TypeError.
    • Even after fixing with is_array($variables['content']), some blocks in footerblocks are never flagged, and I get logs like: Block b5subtheme_belangrijkelinks_3 processed. Region: NONE. (No footerblocks flag found)
  2. Trying to detect region in preprocess_block()
    • I check $variables['elements']['#configuration']['region'] or $variables['elements']['#block']->getRegion().
    • This works for some blocks but not reliably for all, and I’m not sure if I’m missing something about render arrays or region naming.
  3. Using block--footerblocks.html.twig I considered creating a custom block template for the footerblocks region (e.g., block--footerblocks.html.twig) and adding the Bootstrap classes there. Problem: Some blocks still don’t render with the correct classes, and I want a method that works for all blocks automatically, without creating a separate template for each block.

Questions:

  • What is the best/reliable way in Drupal 11 to detect if a block is placed in a specific region?
  • Is there a safer way than preprocess_region() + flags to automatically add classes to all blocks in a region?
  • Could there be a problem with how my region footerblocks is defined or rendered in a Bootstrap 5 subtheme?

Thanks in advance for any guidance or examples!

submitted by /u/Prof-ChromeOS
[link] [comments]
❌