0

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';
  }

}

1 Answer 1

0

Check Block Class module for this

Block Class allows users to add classes to any block through the block's configuration interface. By adding a very short snippet of PHP to a theme's block.tpl.php file, classes can be added to the parent element of a block. Hooray for more powerful block theming!

1
  • thank your answer. But I wanna make to programmatically change classes when region is has some block and not for drpual 7. Nov 29, 2016 at 4:32

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.