- Can every theme function in include/theme.inc be overridden by a template (*.tpl) file?
- Can preprocess functions be used to add variables to any theme function?
-
What is it you're asking, exactly? Your question doesn't have any context, so it's hard for anyone to offer any help.– Damien WilsonCommented Nov 29, 2010 at 2:26
4 Answers
Theming hooks (templates and theme functions) defined in hook_theme()
implementations are always overridable. But they have to be used properly, always use theme('foo', $bar)
, never theme_foo($bar)
.
In Drupal 6, (pre)process functions are only available for templates, not theme functions. Also, the templates must be be overriden in the theme otherwise the preprocess function from the theme is not used. So to have your THEME_preprocess_node(&$variables)
function used, you need to have the node.tpl.php
in your theme folder.
Another limitation (actually a bug) is that for a template to be overridable in a theme, the file name (without the .tpl.ph
p extension) must be exactly the same as the theming hook name except for the _
that are replaced by -
:
function MODULE_theme() {
return array(
'overridable_template' => array(
'template' => 'overridable-template',
),
'non_overridable_template' => array(
'template' => 'nonoverridable-template',
)
);
}
- Yes, but you have to register the template file by implementing hook_theme.
- No, according to http://drupal.org/node/223430, "Preprocess functions only apply to theming hooks implemented as templates".
-
-
@tim.plunkett Yes, this is only correct for versions of Drupal that have already been released by the time I answered this question.– OswaldCommented Jun 2, 2011 at 0:12
-
Whoaaaa didn't notice this was migrated from stackoverflow. That makes much more sense now! Sorry if I seemed abrupt. Commented Jun 2, 2011 at 1:56
1) Yes.
2) In Drupal 7, there is the new hook_preprocess()
, which can be used for any theme or template function. For example, theme_checkbox()
can be preprocessed by YOURTHEME_preprocess_checkbox()
.
- Yes, they can. As with any theme function defined from other modules, you can override them with a template file if you implement hook_theme_registry_alter().
- As reported in the documentation for theme()
If the implementation is a template file, several functions are called before the template file is invoked, to modify the
$variables
array. These fall into the "preprocessing" phase and the "processing" phase, and are executed (if they exist), in the following order (note that in the following list, HOOK indicates the theme hook name, MODULE indicates a module name, THEME indicates a theme name, and ENGINE indicates a theme engine name):