8.3.x theme.api.php hook_preprocess_HOOK(&$variables)
8.0.x theme.api.php hook_preprocess_HOOK(&$variables)
8.1.x theme.api.php hook_preprocess_HOOK(&$variables)
8.2.x theme.api.php hook_preprocess_HOOK(&$variables)
8.4.x theme.api.php hook_preprocess_HOOK(&$variables)
6.x core.php hook_preprocess_HOOK(&$variables)
7.x theme.api.php hook_preprocess_HOOK(&$variables)

Preprocess theme variables for a specific theme hook.

This hook allows modules to preprocess theme variables for a specific theme hook. It should only be used if a module needs to override or add to the theme preprocessing for a theme hook it didn't define.

For more detailed information, see theme().


$variables: The variables array (modify in place).

72 functions implement hook_preprocess_HOOK()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

bartik_preprocess_block in themes/bartik/template.php
Override or insert variables into the block template.
bartik_preprocess_html in themes/bartik/template.php
Add body classes if certain regions have content.
bartik_preprocess_maintenance_page in themes/bartik/template.php
Implements hook_preprocess_maintenance_page().
bartik_preprocess_node in themes/bartik/template.php
Override or insert variables into the node template.
garland_preprocess_block in themes/garland/template.php
Override or insert variables into the block template.

... See full list


modules/system/theme.api.php, line 159


function hook_preprocess_HOOK(&$variables) {
  // This example is from rdf_preprocess_image(). It adds an RDF attribute
  // to the image hook's variables.
  $variables['attributes']['typeof'] = array('foaf:Image');


pfrenssen’s picture

When defined in a module it is not possible to append template suggestions to the hook.

This means you can use MODULE_preprocess_links() but not MODULE_preprocess_links__locale_block().

Ref. #939462: Specific preprocess functions for theme hook suggestions are not invoked.

aronne’s picture

it's not true, i'm using this hook just for add template suggestions.
try this way:

 * Implementation of hook_preprocess_HOOK().
function mymodule_preprocess_page(&$variables){
  if(isset($variables['node']) && arg(2) != 'edit')
    $variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;

It works in the same way even with node template suggestions by using mymodule_preprocess_node(&$variables).

mester9’s picture

What if I want to use new template file in a custom foler, let's say:
/sites/all/modules/custom/MYMODULE/theme/page.tpl.php in the modules theme folder...

how do I do that?

nguyentran’s picture

I'm working on a custom module and I want to keep my template file in my module directory (as opposed to a theme directory)

This approach worked for me: http://www.metachunk.com/blog/adding-module-path-drupal-7-theme-registry...

scito’s picture

My work around until #939462: Specific preprocess functions for theme hook suggestions are not invoked is fixed: Implement preprocess_links and check for language switcher links.
My example for setting the iso language code in the language switcher block.

 * Implements hook_preprocess_HOOK().
function MY_MODULE_preprocess_links(&$variables) {
  // Hack: Check if class 'language-switcher-locale-url' is set for the links
  if (isset($variables['attributes']['class']['0']) && $variables['attributes']['class']['0'] == 'language-switcher-locale-url') {
    foreach (array_keys($variables['links']) as $langcode) {
      $variables['links'][$langcode]['title'] = $langcode;

I've required some time until I've found this workaround.

rafinskipg’s picture

In themename_preprocess_page(&$vars)

You can use the following code in order to se if the region is empty or not:


In drupal 6 it was

barraponto’s picture

Because regions are now renderable arrays, it might be populated but not print anything if the elements are set to hide().

delphian’s picture

Use array_keys(theme_get_registry()) to view the existing possible replacements for _HOOK

ttkaminski’s picture

I tried creating a module that implements a preprocess hook for a template that I defined in the theme's template.php file. This combination doesn't seem to work for me. Not sure if it's a bug or this is by design.

majid008’s picture

[module]_preprocess_[theme hook name](&$variables)

So if the foo module wants to alter the variables for the item_list theme hook , it could define the function as follows:

function foo_preprocess_item_list(&$variables) {
  // Add a class to the list wrapper.
  $variables['attributes']['class'][] = 'foo-list';