Allow subthemes to have preprocess hooks without tpl files.

JohnAlbin - April 21, 2008 - 22:54
Project:Zen
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Buried in the D6 theme guide is this note:

In order for your theme to have its preprocessors recognized, the template associated with the hook must exist inside the theme. When a default template exists, copy it to your theme and clear the registry.

This would mean that a sub-theme would have to have copy of all of Zen core’s .tpl.php files. Unless we can figure out a better way to handle it.

When I talked to Earl about this, he said…

Hmm, in D6 you'd implement hook_theme, run the auto discovery function and get its results; then do your own check for theme_* functions. If the hook was already discovered, ignore it; if it wasn't, add it to your list and make sure you keep path information

…which I will have to translate into actual code and play around with.

#1

JohnAlbin - May 1, 2008 - 00:57

Initial notes:

<?php
function zen_theme($existing, $type, $theme) {
 
$new['page'] = $existing['page'];
 
$new['node'] = $existing['node'];
 
$new['comment'] = $existing['comment'];
 
$new['block'] = $existing['block'];
 
$new['block']['preprocess functions'][] = 'zen_preprocess_block'; // If we change phptemplate_preprocess_block to zen_preprocess_block
 
return $new;
}
?>

#2

Steven_NC - May 1, 2008 - 17:52

I am going to show my ignorance here. What would happen if Zen always had a sub-theme and was factored for that?

#3

JohnAlbin - May 12, 2008 - 23:02
Status:active» patch (code needs review)

Ok. I tried to return the preprocess functions from a HOOK_theme() call, but _theme_process_registry() sees the new SUBTHEME_preprocess_HOOK function, looks for a HOOK.tpl.php in the subtheme and, when it doesn't find it, assumes you must want to use a SUBTHEME_HOOK() function to build the template. And, thus, it all falls apart.

The documention for HOOK_theme shows this:

<?php
function SUBTHEME_theme($existing, $type, $theme, $path) {}
?>

Fortunately, I discovered that if I implement it like so:

<?php
function SUBTHEME_theme(&$existing, $type, $theme, $path) {}
?>

I can manipulate the registry cache ($existing) directly. Sweet. Not sure if I'm abusing the HOOK_theme() call, but its the only way to fix this issue.

I'm attaching the patch for reference.

AttachmentSize
hook-theme.patch8.08 KB

#4

JohnAlbin - May 12, 2008 - 23:01
Status:patch (code needs review)» fixed

Committed.

#5

JohnAlbin - May 13, 2008 - 19:52
Status:fixed» active

Right now, the patch allows sub-themes to have preprocess functions for any of the preprocess functions used in the base Zen theme (page, node, block, comment). But I think it would be good to expand this to allow preprocess functions for any HOOK that has a tpl file (regardless if the tpl is in the Zen theme or not.)

#6

JohnAlbin - May 14, 2008 - 14:40
Status:active» fixed

Ok. I've optimized the code and expanded it to apply preprocess functions to ALL hooks, not just the four used in zen's template.php page.

#7

andershal - May 15, 2008 - 13:35

Hey JohnAlbin, could you attach your updated patch?

#8

JohnAlbin - May 15, 2008 - 15:45

The patch in #6 is available here: http://cvs.drupal.org/viewvc.py/drupal/contributions/themes/zen/zen/temp...

Of course, it patches the code that was patched in #4. If you want the entire code, the heart of the patch is the HOOK_theme() hook function; you can find the entire hook (called zen_theme, of course) in zen/template.php in the 6.x-1.0-beta1 release.

#9

Anonymous (not verified) - May 29, 2008 - 15:56
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.