4
  • 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?

migrated from stackoverflow.com Jun 1 '11 at 17:31

This question came from our site for professional and enthusiast programmers.

  • 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 Wilson Nov 29 '10 at 2:26
  • i am sorry,i forgot to say it's about drupal. – runeveryday Nov 29 '10 at 2:29
1
  1. Yes, but you have to register the template file by implementing hook_theme.
  2. No, according to http://drupal.org/node/223430, "Preprocess functions only apply to theming hooks implemented as templates".
  • This is only correct for Drupal 6 and below. – tim.plunkett Jun 1 '11 at 21:10
  • @tim.plunkett Yes, this is only correct for versions of Drupal that have already been released by the time I answered this question. – Oswald Jun 2 '11 at 0:12
  • Whoaaaa didn't notice this was migrated from stackoverflow. That makes much more sense now! Sorry if I seemed abrupt. – tim.plunkett Jun 2 '11 at 1:56
1

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.php 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',
    )
  );
}
1

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

0

  • 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):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.