Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a block which template I'm overriding with my own.

The title in it is displayed by the following code:

    <h2<?php print $title_attributes;?>><?php print $title; ?></h2>

I just want to add some custom classes to the h2, but I can't figure out how to format it. I want to keep $title_attributes but I can't seem to be able to add my own classes if I do.

I tried:

    <h2 class="a-class" <?php print $title_attributes;?>><?php print $title; ?></h2>

and:

    <h2<?php print $title_attributes .'a-class';?>><?php print $title; ?></h2>

But neither did anything. My PHP is scrappy at best so it's probably just me who don't know how to write what I want. Can anyone help me?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

The way to do this is by overriding variables in the template. Put the following code in your themes 'template.php' file, replacing YOURTHEME, YOURBLOCK, YOURCLASS with the obvious.

/**
 * Override or insert variables into the block template
 */
function YOURTHEME_preprocess_block(&$vars) {

  // var_dump($vars);

  $block = $vars['block'];

  if ($block->delta == 'YOURBLOCK') {
    $vars['title_attributes_array']['class'][] = 'YOURCLASS';
  }

}

You can determine what the $block->delta is for example by doing a var_dump($vars) in the function. If you have the DEVEL module installed you can use dsm($vars) which gives the dump some presentation.

share|improve this answer
    
Ok. So there is no way to do this in the presentational markup? I'd like to keep my code relating to a certain block in the same place if I can, and not spread out among several files. Maybe this is the best way to do it, and it's just me with my Wordpress background that don't feel very good about this method. –  Johan Dahl Feb 27 '13 at 9:04
    
At the end of the day its a framework, there are best practices for working with the framework. In general you should keep logic separate from presentation. Adhering to this sort of methodology well help you going forward. If you wanted to do it your way, I would suggest creating a separate template specifically for the block, removing the $title_attributes variable and hardcoding the class into the template. Wouldnt recommend it though. –  Alex Gill Feb 27 '13 at 9:14
    
Ok. Thank's for helping out. –  Johan Dahl Feb 27 '13 at 9:39
    
No worries, glad to help! –  Alex Gill Feb 27 '13 at 9:45

You may use the Block Class module.

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!

share|improve this answer
    
I already use this module to put classes on the block itself. I can't see anywhere where I can put classes on the rendered elements inside the block. Am I missing something? –  Johan Dahl Feb 27 '13 at 9:00
    
This is where the CSS descendant selector comes in real handy. E.g. I use the block class module to manually add a class .block--has-subhead on a per block basis. Then in my stylesheet I have a rule like .block--has-subhead h2 { font-size: 90%; }. I prefix all of my manual block classes with block--. It's an invaluable cue to remind me that that the class name is set manually in the ui (vs core, or some module, or theme. I use a similar syntax when I add classes through the views ui, display_suite, panels, etc. –  Zach Harkey Feb 19 at 19:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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