4

I need to add a class to the h2 block titles in a sidebar. How can I do this in pre-process? I haven't been able to find an example that shows how to isolate certain blocks. I'm using Omega.

1 Answer 1

8

template_preprocess_block()

Inspect the block object in this preprocess function. You could use either the 'module' and 'delta' properties to target a specific block or the 'region' property to target all blocks in a certain region.

H2 css-classes are listed in the title_attributes_array. The code below shows how to add a new class. This example is based on a block generated by Views:

function MYTHEME_preprocess_block(&$variables) {
  $block = $variables['block'];
  if ($block->module == 'views' && $block->delta == 'name_of_my_view-block') {
    $variables['title_attributes_array']['class'][] = 'my-new-class';
  }
}

Sidenote: dpm() by the Devel module is a handy tool for printing out the available data in variables:

function MYTHEME_preprocess_block(&$variables) {
  dpm($variables);
}
2
  • Thanks. That did it! Question - is the $variables['block'] value a ref to the $variables['elements']['#block'] array?
    – vintorg
    Commented May 2, 2013 at 6:22
  • Can I wrap the h2 tag in another div, or do I need to do that in a block template file? I need to style the h2 titles with a ribbon using CSS.
    – vintorg
    Commented May 2, 2013 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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