module_preprocess_hook not always called
Is there a reason why a module's preprocess function is not always called? I have several preprocess functions in a module, and I'd like to preprocess certain pieces of themed content based on content types specific to this module - so, it makes sense to me that the preprocess code should be in the module file. I've traced through the theme() call, and my module's preprocess function name appears in the $hooks array. For example, i'd like to hilight a menu, so I'm trying to preprocess theme(links). My theme has a theme_links function which generates the html output, but in this case the preprocess function is NOT called. For other pieces of themed output, using hook.tpl.php files, the preprocess functions ARE called. Is there a reason why? -Matt
Template preprocess functions are, as the name implies, only for templates. -Charlie Matt Connolly wrote:
My theme has a theme_links function which generates the html output, but in this case the preprocess function is NOT called.
For other pieces of themed output, using hook.tpl.php files, the preprocess functions ARE called.
Matt Connolly wrote:
Is there a reason why a module's preprocess function is not always called?
The answer to your question is right in the theming documentation: http://drupal.org/node/223430
No. My question is WHY? I can see that the preprocess functions are called for theme files and not for functions. I think it would be useful for everything. eg, quote (from http://api.drupal.org/api/function/theme) : -- For example, if a module named "foo" wanted to alter the $submitted variable for the hook "node" a preprocess function of foo_preprocess_node() can be created to intercept and alter the variable -- That seems like pretty useful functionality regardless of whether the content is generated by a function or a template file. Another email informing me about "hook_theme_registry_alter" was very useful - thanks. -Matt On 29/07/2008, at 5:17 PM, Earl Miles wrote:
Matt Connolly wrote:
Is there a reason why a module's preprocess function is not always called?
The answer to your question is right in the theming documentation: http://drupal.org/node/223430
Matt Connolly wrote:
No. My question is WHY?
For one, you really can't use the preprocessors with a theme function. With a template, you get a nice handy box of variables and you can do what you want with it. With a theme function, you get the variables that were defined, and that's it. Sure, theoretically you could massage those X variables a bit. The real reason, though, is performance. Templates take up to five times as long to run as theme functions; and Drupal has a couple of places where a single theme function might be called fifty or a hundred times on a page. This is discussed here: http://drupal.org/node/173880 and there is another mention of it here in the module developer's guide where we discuss that side of using the theme layer: http://drupal.org/node/165706
participants (4)
-
Charlie Gordon -
Earl Miles -
Karoly Negyesi -
Matt Connolly