What's the difference between these ones in function names? I can overwrite each one in template.php, so?

  • You've missed a bit off that...it's template_process_/template_preprocess_. That's the difference - one is a theme function, one is process/preprocess function for the theme – Clive Jul 18 '13 at 13:51
  • So they used the word "template" instead of "theme" only to avoid names conflicts? – deem Jul 18 '13 at 13:55
  • 1
    I'm not sure, it's a good question! I can't comment on the motivation for naming those functions (they go back a few Drupal versions, before my time with the CMS) but I think it's a throwback from Drupal 5/6 php_template conventions or something similar. I've never questioned why they did it, just learnt which one to use when and where :) – Clive Jul 18 '13 at 13:58
  • What makes it slightly more confusing is that you can use MYMODULE_ instead of template_ or theme_ in a custom module for any module you write. So, essentially you can treat either of these as hook_ in a module. This isn't something I have a good grip on myself. I think I need to re-read @kiamlaluno's response a time or two more. Interesting question. – cdmo Jul 23 '13 at 20:57

The functions whose name start with theme_ are theme hooks, while the functions whose name start with template_ are preprocess and process functions implemented by the module that implements the relative theme hook.

For example, theme_image() is a theme hook that renders an image. template_preprocess_node() is the preprocess function implemented by the Node module, which the module implementing the node theme hook. (See the first code lines of node_theme(), the hook_theme() implementation done by the Node module.) There are other modules that can implement a preprocess function for the node theme hook, but for those modules, that preprocess function is named MYMODULE_preprocess_node(), not template_preprocess_node().

A theme can implement both a theme hook (see bartik_menu_tree()) and a preprocess function (see bartik_preprocess_node()). The difference is that bartik_menu_tree() overrides the default implementation (theme_menu_tree()) when the current theme is the Bartik theme, while bartik_preprocess_node() doesn't override template_preprocess_node(), but it is executed after it. (See the documentation for theme().)

As for using two prefixes, the reason is to documenting the purpose of the function. Even if theme_ were used in both the cases, the difference between theme_image() and theme_preprocess_node() would still be clear, but it would require you to read what follows theme_. With the current implementation, you just read the first word of the function name.

| improve this answer | |

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.