How to add a class to a Drupal 7 region?
Asked Answered
C

4

9

I am trying to add a .clearfix class to my footer region in a Drupal 7. Is there a way to do this?

I am currently using the following to print my footer region:

<?php print render($page['footer']); ?>

Which outputs:

<div class="region region-footer">
   <div id="block-1>....</div>
   <div id="block-2>....</div>
</div>
Czerny answered 20/7, 2011 at 11:58 Comment(1)
Are you just trying to change the css, or are you trying to include some php?Rurik
Q
7

Copy region.tpl.php (found in modules/system directory) to your theme directory. Then copy everything inside it and create a new file. Paste into that file and make any changes you like to the template. Once finished, save it as region--footer.tpl.php and clear the cache on your site to see the changes.

The region.tpl.php contains (along with a lot of comments explaining possible variables):

<?php if ($content): ?>
  <div class="<?php print $classes; ?>">
    <?php print $content; ?>
  </div>
<?php endif; ?>

So all you would need to do is add a class on that DIV.

Queue answered 20/7, 2011 at 13:9 Comment(3)
Perfect, I didn't know you could copy system templates into your own theme folder. Thanks Laxman13!Czerny
Sorry for asking, I've did the same way and it works. But it removes some divs (#footer-wrapper, #footer-inner) from the footer region. And it also removes the id footer from the div where the classes are added. Is it correct? or has it something to do with a custom theme I'm using.Kava
I would guess that has to do with the custom theme you are using. It might have its own region templates (specifically footer). In that case, you could change that template instead.Queue
C
34

Here's the code snippet:

function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
    if($variables['region'] == "MY_REGION_NAME"){
        $variables['classes_array'][] = 'MY_CLASS_NAME';
    }
}

Or if you'd rather insert the class into all of the regions:

function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
    $variables['classes_array'][] = 'MY_CLASS_NAME';
}
Cuneo answered 10/1, 2013 at 2:35 Comment(0)
Q
7

Copy region.tpl.php (found in modules/system directory) to your theme directory. Then copy everything inside it and create a new file. Paste into that file and make any changes you like to the template. Once finished, save it as region--footer.tpl.php and clear the cache on your site to see the changes.

The region.tpl.php contains (along with a lot of comments explaining possible variables):

<?php if ($content): ?>
  <div class="<?php print $classes; ?>">
    <?php print $content; ?>
  </div>
<?php endif; ?>

So all you would need to do is add a class on that DIV.

Queue answered 20/7, 2011 at 13:9 Comment(3)
Perfect, I didn't know you could copy system templates into your own theme folder. Thanks Laxman13!Czerny
Sorry for asking, I've did the same way and it works. But it removes some divs (#footer-wrapper, #footer-inner) from the footer region. And it also removes the id footer from the div where the classes are added. Is it correct? or has it something to do with a custom theme I'm using.Kava
I would guess that has to do with the custom theme you are using. It might have its own region templates (specifically footer). In that case, you could change that template instead.Queue
I
4

It is even better if you use a hook, you can use template_preprocess_region.

Influx answered 5/12, 2012 at 19:57 Comment(0)
W
0

Try adding the include to the footer.php.tpl file. You may have to create it.

Wristwatch answered 20/7, 2011 at 12:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.