Block Class

Todd Nienkerk - April 14, 2008 - 16:05

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 <div class="block ..."> element of a block. Hooray for more powerful block theming!

Installation

  1. Enable the module
  2. Add <?php print block_class($block); ?> to your theme's block.tpl.php file (see detailed instructions below).
  3. To add a class to a block, simply visit that block's configuration page at Admin > Site Building > Blocks.

How to add the PHP snippet

Here's the first line of the Garland theme's block.tpl.php prior to adding the code:

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">

And here's what the code should look like after adding the code:

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?> <?php print block_class($block); ?>">

IMPORTANT: Remember to separate the PHP snippet from the existing markup with a single space. If you don't add the space, your CSS classes could run together like this: block-modulecustomclass instead of block-module customclass.

Checking if the function block_class() exists before calling it

If there's a chance you may disable the Block Class module, you should consider placing the PHP snippet inside a conditional statement that checks to make sure the function actually exists before calling it: <?php if (function_exists(block_class)) print block_class($block); ?>. This will prevent a nasty "Call to undefined function" error.

Why use function_exists() instead of Drupal's handy module_exists() function? It's faster!

Credits

Development of this module is sponsored by Plum TV and Four Kitchen Studios.

Releases

Official releasesDateSizeLinksStatus
5.x-1.12008-Jun-247.1 KBRecommended for 5.xThis is currently the recommended release for 5.x.
 
 

Drupal is a registered trademark of Dries Buytaert.