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().

Parameters

$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

File

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

Code

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');
}

Comments

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:

!empty($vars['page']['sidebar_first'])

In drupal 6 it was

if(isset($vars['region_name]))
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';
}



theme('item_list',$vars),theme('table',$vars),theme('username',$vars)
,theme('breadcrumb',$vars),theme('image',$vars),theme('links',$vars)
,theme('status_messages',$vars),theme('progress_bar',$vars),...

Pol’s picture

Sorry to dig up very old topics... but some companies are still working with Drupal 7 unfortunately... and some of them are still creating themes...

So, with Atomium, you are able to have preprocess and process functions for custom theme() calls per default.
We have included this feature in the theme, so you don't have to worry.

Examples:

theme('link__variant1__variant2', array(...));

The original hook is link and the preprocess functions that will be triggered, if they exists are:

  1. HOOK_preprocess_link()
  2. HOOK_preprocess_link__variant1()
  3. HOOK_preprocess_link__variant1__variant2()

Another example:

theme('link__variant1__variant2__variant3', array(...));
  1. HOOK_preprocess_link()
  2. HOOK_preprocess_link__variant1()
  3. HOOK_preprocess_link__variant1__variant2()
  4. HOOK_preprocess_link__variant1__variant2__variant3()

This is super great because it allows you to create, just by naming properly a function, a preprocess that will be common to those two links very easily.

Do not hesitate to ping me if you need any help.