Support Center

Preprocess variables for Drupal block.tpl.php

Last Updated: Sep 05, 2013 05:49PM EEST
The function template_preprocess_block prepares the values passed to the theme_block function to be passed into a pluggable template engine. You can see more at the corresponding Drupal API page.

In your template.php (sites/all/themes/THEMENAME/template.php), you can preprocess the variables to make alterations before block.tpl.php* serves them up. You can do this using the hook_preprocess_block(). 
 

How to add a class to your blocks using hook_preprocess_block()

With the following example, you can add the class "clearfix" to your system blocks. In your function, you'll replace hook with your theme name:
 
function THEMENAME_preprocess_block(&$variables) {

$variables['classes_array'][]='clearfix';
    
}
 

How to add a class to the title of your blocks using hook_preprocess_block()

With the following example, you can add the class "title" to the title (h2 element) of your system blocks. In your function, you'll replace hook with your theme name:
 
function THEMENAME_preprocess_block(&$variables) {

$variables['title_attributes_array']['class'][] = 'title';
    
}

Finally, you should clear all caches through Administration » Configuration » Performance » Clear all caches. This step is important in order to force your template to be informed for the changes you have made in the template files.

*Most themes utilize their own copy of block.tpl.php. The default is located inside modules/block/block.tpl.php.

In case this article leaves any of your questions or concerns unanswered, please feel welcome to email us or post a public question.

​More than (just) Themes
http://www.morethanthemes.com/

Contact Us

support-agent@morethanthemes.com
http://assets2.desk.com/
false
morethanthemes
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete