I am trying to add a class to the block title of blocks in a certain region. Although some information is added to the HTML, it stands alone and is not included in the class="". See HTML output below:
<h2 class="block-title" 0="invisible-elements">Navigation bar top</h2>

I am using a fresh Drupal 7.2 installation with a Zen Theme. My startertheme is named "test". The name of the region is "navbar_top". I included the following code into my template.php.

function test_preprocess_block(&$variables) {
	if($variables['block']->region == 'navbar_top') : 
	$variables['title_attributes_array'][] = 'invisible-elements';	
	endif; 
}

The default Zen block.tpl.php is used. see html code below:

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

I tried to use theme developer to find out the variable names. Strangely, "title_attributes" and "title_attributes_array" remain empty. I am not certain in which direction to look to solve my question.

NB: Is the ['block']->region piece the correct way to select the region a block resides in?

Comments

nevets’s picture

Note that most themes give the wrapper div for the region a css id. So using your example a css rule like

#navbar-top .block h2 {
  ...
}

would apply to all h2 tags in blocks that are in the navbar_top region.

oeroek’s picture

I also thought of a CSS solution. It would be fairly easy to solve it that way. However, it would also lead to excess CSS rules and html. I could also remove the $block->subject entirely via:
$variables['block']->subject == NULL

The thing is that this afternoon I was trying to learn more about using preprocessing functions in combination with title_attributes. I understand that it is added for use with rdf implementation and could be perfectly used to add classes. Nevertheless I seem to be unable to manipulate the classes this way.

If anybody knows a website with best practice code snippets and/or a decent tutorial please comment.

nevets’s picture

Don't see why it would lead to excess css or html. Still would only take on css rule.

As for your code I would guess

$variables['title_attributes_array'][] = 'invisible-elements';

should be

if ( empty($variables['title_attributes_array']['class']) ) {
  $variables['title_attributes_array']['class'] = 'invisible-elements';
}
else {
  $variables['title_attributes_array']['class'] .= ' invisible-elements';
}

oeroek’s picture

Thanks for your comments, I really appreciate your help. Regarding excess css and html, I use shortcuts in css and even html (strpos, str_replace) regularly. That is what casuses excess code and eventually a complex not flexible system. That's why I am looking into other alternatives and more elegant coding.

Your solution above changes the html output into:
<h2 class="Array invisible-elements">Navigation bar top</h2>

It brought me on the right track however. The code below does the job:

 $variables['title_attributes_array']['class'][] = 'element-invisible';