| 7 theme.api.php | hook_preprocess_HOOK(&$variables) |
| 6 core.php | hook_preprocess_HOOK(&$variables) |
| 8 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).
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
Does not work with template suggestions
PermalinkWhen 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.
it works ;)
Permalinkit'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).
what if....
PermalinkWhat 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?
Hope this work for you
PermalinkI'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...
Workaround for links__locale_block implementation
PermalinkMy 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.
<?php/**
* 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.
Need to look up for an empty region...? Use this
PermalinkIn 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]))No you can't
PermalinkBecause regions are now renderable arrays, it might be populated but not print anything if the elements are set to hide().
_HOOK Possible Values
PermalinkUse array_keys(theme_get_registry()) to view the existing possible replacements for _HOOK
Preprocess in module for templates defined in theme
PermalinkI 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.