I have one row three column region. How can I add custom class on each region when one of region is not work?
When three regiosn work, classes will be below
|------------|-------------|--------------|
| | | |
| region 1 | region 2 | region 3 |
| | | |
|------------|-------------|--------------|
col-sm-4 col-sm-4 col-sm-4
When two regions work, classes will be below
|-------------------|---------------------|
| | |
| region 1 | region 2 |
| | |
|-------------------|---------------------|
col-sm-6 col-sm6
When only one region work, class will be below
|-----------------------------------------|
| |
| region 1 |
| |
|-----------------------------------------|
col-sm-12
I use in page.html.twig
like below. It worked for me but not good code for more column.
{% if page.region_1 and page.region_2 %}
<div class="row">
<div class="region_1 col-sm-6"> {{ page.region_1 }} </div>
<div class="region_2 col-sm-6"> {{ page.region_2 }} </div>
</div>
{% endif %}
{% if not page.region_1 and page.region_2 %}
<div class="region_2 col-sm-12"> {{ page.region_2 }} </div>
{% endif %}
{% if not page.region_2 and page.region_1 %}
<div class="region_1 col-sm-12"> {{ page.region_1 }} </div>
{% endif %}
I'm try using hook_preprocess_region()
like below. Can I check regions condition in hook_preprocess_region
?
function MYTHEME_preprocess_region(&$variables) {
// Add class in header region.
$region = $variables['elements']['#region'];
$region_header = array(
'region_1',
'region_2',
'region_3'
);
if (in_array($region, $region_header)) {
$variables['attributes']['class'][] = 'col-sm-12';
}
}