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 this snippet to your theme's block.tpl.php file (see detailed instructions below): <?php print block_class($block); ?>
  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 snippet:

<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

Four Kitchens logo

Development of this module is sponsored by Four Kitchens.

Releases

Official releasesDateSizeLinksStatus
6.x-1.22009-Jun-038.34 KBRecommended for 6.xThis is currently the recommended release for 6.x.
5.x-1.12008-Jun-247.1 KBRecommended for 5.xThis is currently the recommended release for 5.x.
Development snapshotsDateSizeLinksStatus
6.x-2.x-dev2009-Jun-049.33 KBDevelopment snapshotDevelopment snapshots are automatically regenerated and their contents can frequently change, so they are not recommended for production use.


 
 

Drupal is a registered trademark of Dries Buytaert.