I need to add a css class to my blocks and I know I can install a module, but i rather not as it doesnt work with features. I was thinking of just using the block config's description field to hold my css classes and just output it in my block.tpl.php file or should i do a preprocess thing?

Now the question is what is the variable to output the block's "description"?

  • All blocks have already unique id's and classes derived from those id's. Why not using them? – duru Apr 19 '15 at 17:36
  • because if your on a dev environment and you migrate your code to production. the ids will be different as the prod environment data changes if someone creates a new block. so block id's arent a reliable selector. – Patoshi パトシ Apr 19 '15 at 17:41
  • cool:) a var_dump() showed that the description is not passed to block.tpl.php at all. So I would go with the preprocess thingy:) – duru Apr 19 '15 at 18:08
  • I looked into it, you need to do some nasty coding if you want to reach the description in preprocess. I rather suggest using block title directly under tpl.php with some toLower() function. If you are okey with that, I can look again and write the code. – duru Apr 19 '15 at 19:31

If I understood your question correctly, you have two options:

A) Use a _preprocess_block() function:

function THEME_preprocess_block(&$vars) {
  // Make sure that this is a custom block, and not a block that is provided by
  // a module.
  if ($vars['block']->module == 'block') {
    // Load the block information.
    $info = block_custom_block_get($vars['block']->delta);
    // Add block description to the list of classes.
    $vars['classes_array'][] = $info['info'];

B) Use the Skinr module. Probably will require more work, but it's much more flexible and editors / non-tech staff will be able to define styles of blocks. From the project page:

Skinr's main purpose is to allow the theme to define a set of reusable and modular CSS styles, and to make those styles available in Drupal's UI.

  • but how do u output the "description" field ? – Patoshi パトシ Apr 19 '15 at 18:50
  • Answer updated. – Aram Boyajyan Apr 19 '15 at 20:17
  • This will work on custom blocks only. – duru Apr 19 '15 at 22:18
  • Yes, because blocks provided by other modules do not have the "Description" field, only "Block title". In either case, what OP wants is not an optimal solution. Skinr is an optimal solution though. – Aram Boyajyan Apr 20 '15 at 7:02
  • i rather not install anymore modules that i have already. a simple code snippet would do. – Patoshi パトシ May 27 '15 at 22:59

You could try the Block Class module, seems relatively popular and well maintained:

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!

To add a class to a block, simply visit that block's configuration page at Administration > Structure > Blocks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.