Parent theme pattern clobbering

neclimdul - November 11, 2008 - 19:30
Project:Drupal
Version:6.x-dev
Component:theme system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

So, I'm not entirely sure I know how to express this bug so here's the example using views 2.

When you have a parent theme has a general template file like views-view-field.tpl.php and a child theme has a more specific template such as views-view-field--viewname--fieldname.tpl.php, the more specific template will not be detected.

Attached is a patch suggested by merlinofchaos as a solution.

AttachmentSize
parent_theme_pattern_clobbering.patch767 bytes
Testbed results
parent_theme_pattern_clobbering.patchpassedPassed: 7517 passes, 0 fails, 0 exceptions Detailed results

#1

dvessel - November 11, 2008 - 19:38

Will dig into this soon...

#2

dvessel - November 12, 2008 - 03:23
Status:needs review» reviewed & tested by the community

The patch works perfectly. Tested up to 3 level of subthemes. Tweaked the docs and fixes offset..

AttachmentSize
parent_theme_pattern_clobbering.b.patch 876 bytes
Testbed results
parent_theme_pattern_clobbering.b.patchre-testing

#3

Gábor Hojtsy - November 17, 2008 - 10:33
Version:6.x-dev» 7.x-dev

Please get this into 7 first.

#4

Damien Tournoud - November 17, 2008 - 11:48
Status:reviewed & tested by the community» needs work

What's the link with #279573: Themes can't use node-story.tpl.php without node.tpl.php? Are those duplicate?

Putting as code needs work, as this will need a test case before getting in 7.

#5

dvessel - November 19, 2008 - 00:12
Status:needs work» reviewed & tested by the community

Damien, the template suggestions and this issue is unrelated. That issue depends on theming paths which depends on the base template.

AttachmentSize
parent_theme_pattern_clobbering.b-head.patch 861 bytes
Testbed results
parent_theme_pattern_clobbering.b-head.patchpassedPassed: 7490 passes, 0 fails, 0 exceptions Detailed results

#6

merlinofchaos - November 19, 2008 - 21:45

FYI I helped neclimdul with this original patch, so if it matters I'm very much in favor of it.

In order to describe what happens, we'll still use Views as an example.

We have the theme 'views_view', and through the pattern system, 'views_view__foo' is called. During ordinary operations, views-view--foo.tpl.php will be found and utilized.

However, there is an obscure situation in which a base theme having 'views-view.tpl.php' will register that template and inadvertently destroy the 'pattern'. When the child theme goes to register 'views_view' as a theme, it won't see the pattern, and this it won't search for 'views-view--foo.tpl.php' through the normal mechanism. Thus the theme file is not found.

This patch corrects it; it should be applied to both 7.x and 6.x.

#7

merlinofchaos - November 22, 2008 - 23:27

dvessel: I believe that there needs to be two of these. One in drupal_find_theme_templates and one in its sister function to find theme functions. THey should both do essentially the same thing.

#8

dvessel - November 23, 2008 - 02:03

merlinofchaos, I thought it needed the fix there too and I did test it. I really tried breaking it but works without any patching.

#9

merlinofchaos - November 23, 2008 - 04:04

Do we have any theme functions that work the samew ay though? Having theme_views_view() in a base theme and theme_views_view__foo() in a child theme? I think it should run into the same limitation.

#10

webchick - November 23, 2008 - 04:22
Status:reviewed & tested by the community» needs review

Sounds like this still needs some review.

(also, ninja subscribe ;))

#11

dvessel - November 23, 2008 - 04:49

I thought I tested it throughly but apparently it can slip through when naming the function after the theme engine. Bah!!

#14

dvessel - November 23, 2008 - 05:39

And here's a patch. I also modified the comments. Makes more sense I think.

AttachmentSize
parent_theme_pattern_clobbering-c-D6.patch 1.54 KB
parent_theme_pattern_clobbering-c-HEAD.patch 1.53 KB
Testbed results
parent_theme_pattern_clobbering-c-HEAD.patchre-testing

#15

merlinofchaos - November 23, 2008 - 06:31

Ok, that's more like what I thought. Does the $info['pattern'] in the first part of the patch generate notices? (I'm not sure if it's possible for $info['pattern'] to not be set at that point. If it's registering a new hook that it found from a pattern, $info['pattern'] is always set but I don't think the newly created pattern should actually inherit; that could cause multiple registrations of the same pattern?

#16

dvessel - November 23, 2008 - 07:03

Definitely no notices since that first part is within a "if (!empty($info['pattern'])) {".

http://api.drupal.org/api/function/drupal_find_theme_functions/6

I placed that there since a function named after the pattern could be setup in a base theme in addition to a sub-theme. Without it, only the base theme would catch onto the pattern while the sub-theme would not.

Updated patch fixes a stray word in the comments.

AttachmentSize
parent_theme_pattern_clobbering-d-HEAD.patch 1.52 KB
parent_theme_pattern_clobbering-d-D6.patch 1.53 KB
Testbed results
parent_theme_pattern_clobbering-d-HEAD.patchfailedFailed: Failed to install HEAD. Detailed results

#17

dvessel - November 23, 2008 - 07:08

Merlinofchaos, oh.. I misunderstood. Never mind what I just said. I think your right about the first part and it should be removed.

#18

dvessel - November 23, 2008 - 19:29

Merlinofchaos, I looked through the registry and that first $info['pattern'] did cause newly registered patterned theming hooks to have the same pattern attached. It didn't look harmful since a pattern of "foo__" would register "foo__bar" but "foo__bar" I don't think can further register more patterns on top of that. But I guess it's better to be safe and just remove it.

Everything else looks good to me.

This isn't directly related but I made a few notes to help me grok how patterns fit into the way overrides work.

sub-theme > base theme

hook pattern > base hook
foo--bar.tpl.php vs. foo.tpl.php

function > template
themeName_foo() vs. foo.tpl.php

-----

sub-theme + base hook < base theme + hook pattern

sub-theme + base hook + function < base theme + pattern hook + template

-----

So, patterns have the most weight followed by sub-themes and then theme functions as opposed to templates for a given hook.

AttachmentSize
parent_theme_pattern_clobbering-e-HEAD.patch 1.25 KB
parent_theme_pattern_clobbering-e-D6.patch 1.26 KB
Testbed results
parent_theme_pattern_clobbering-e-HEAD.patchre-testing

#19

merlinofchaos - December 4, 2008 - 22:00
Status:needs review» reviewed & tested by the community

Ironically I think we've come full circle back to my original patch that I gave to neclimdul.

#20

neclimdul - December 5, 2008 - 21:49
Status:reviewed & tested by the community» needs work

Tested on 7. Confirmed to work.

From a discussion with merlin, I created a module with:

<?php
/**
* Implementation of hook_theme().
*/
function test_theme() {
  return array(
   
'foo' => array(
     
'pattern' => 'foo__',
     
'arguments' => array(),
    ),
  );
}
?>

Then create 2 themes with theme functions parent_foo() { print "parent"; } and subtheme_foo__a() { print "subtheme"; }

Before the patch, it prints "parent" and after if prints "subtheme" as is the expected behavior. But, it didn't work for me with templates. Attached is the themes and modules I used to test it in case I made a mistake.

AttachmentSize
pattern_match_test.zip 23.94 KB

#21

dvessel - December 6, 2008 - 21:01
Status:needs work» reviewed & tested by the community

neclimdul, it's because the template must use hyphens. Your test files were using underscores.

#22

neclimdul - December 6, 2008 - 22:49

dope. lets do this thing then!

#23

webchick - December 7, 2008 - 09:33
Version:7.x-dev» 6.x-dev

http://testing.drupal.org/pifr/file/1/parent_theme_pattern_clobbering-e-... gives the patch in #18 a clean pass. We don't (yet) have a way to test theme logic in SimpleTest, but neclimdul's .zip in #20 looks like a really awesome start for when we do.

Committed to HEAD. Thanks! Marking back to 6.x.

#24

webchick - December 7, 2008 - 09:38

#25

Gábor Hojtsy - December 10, 2008 - 21:20
Status:reviewed & tested by the community» fixed

Committed to 6.x, thanks.

#26

Gábor Hojtsy - December 11, 2008 - 02:10

Committed to 6.x, thanks.

#27

System Message - December 25, 2008 - 02:12
Status:fixed» closed

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

#28

momper - January 28, 2009 - 09:27

is this fixed in core now? 6.9?

 
 

Drupal is a registered trademark of Dries Buytaert.