Chris J. Lee

Dallas Drupal Developer

You are here

add block classes with css preprocessing

Webby Kat has a great article on adding css classes with preprocessing. It provides a lightweight alternative to the block class module (which stores the classes in a database).

Adding this snippet in your template or preprocess-block.inc file will provide an easy way to do this:


function MYTHEME_alpha_preprocess_block(&$vars) {
/* Set shortcut variables */
$block_id = $vars['block']->module . '-' . $vars['block']->delta;
$classes = &$vars['attributes_array']['class'];
/* Add classes based on the block delta */
switch ($block_id) {
/* Add .badge class to block #14 */
case 'block-14':
$classes[] = 'badge';
break;
/* Add .form class to block #20 */
case 'block-20':
$classes[] = 'form';
break;
}
}

More resources on this issue:
Adding helpful classes for block preprocessing
Adding css classes the drupal blocks by aten group

© 2017 Chris J. Lee