I am doing templating for a module that adds a block in the module's folder so that the template can be there, and it may be overridden in the theme if desired. I want the module to support different "sources" (but only one will be active/used at a time), which may each have a separate template and variable preprocessing. I have hook_theme() working, and the template works. But the .vars.php file I have contains the preprocess function, and it doesn't get called.
Here is a basic example of what I have:
/MODULE.module
function MODULE_theme($existing, $type, $theme, $path) {
$source = '';
switch ($source) {
case 'SRC_A':
$path .= '/sources/SRC_A/theme';
default:
$source = 'default';
$path .= '/theme';
}
return array(
'MODULE_block' => array(
'path' => $path,
'variables' => array('data' => array(), 'source' => $source),
'template' => 'MODULE-block',
)
);
}
/MODULE/sources/SRC_A/theme/MODULE-block.tpl.php
(contains template HTML with references to PHP variables)
/MODULE/sources/SRC_A/theme/MODULE-block.vars.php
/**
* Implements template_preprocess_HOOK() for theme_MODULE_block().
*/
function template_preprocess_MODULE_block(&$vars) {
// Add variables before they get to the template file.
$vars['some_key'] = 'some_value';
}
I originally was trying to modify the $template
value in hook_theme() to MODULE-block--SRC-A
, but I decided that didn't matter, and if only one template is active at once I should just call them all MODULE-block.tpl.php
regardless of the source, since they are all in different folders.
The template works, and adding a template in my theme will correctly override it. But the .vars.php file with the preprocess function does not seem to do anything unless it is in my actual theme and is THEME_preprocess_MODULE_block
instead.
I'm still pretty new to this, but I looked at a lot of pages online, and there aren't any similar examples in my code base that have a module-specific .vars.php file that is located in the module's folder rather than in the theme.
I do not know what I am doing wrong. Any ideas?
template_preprocess_MODULE_block
, but the function is never called for some reason. I don't know why. I looked using dpm() andMODULE_block
is in the theme registry.