Hi,

While working with the Drupal's theme layer to have a better support of different theme engines in our beloved Drupal 7, Pounard discovered that Drupal's templates must have two dots in their names.

The explanation:

A theme engine may only use a 2-part extension (eg. ".tpl.php" or ".html.twig") but cannot use a single part extension (such as ".phtml" or ".twig"). No one ever saw that comming, because almost no one actually write theme engines, but in the end, this as another side effect: you cannot name your templates "some.thing" (for exemple, that would tie to the "some_thing" theme hook).

I suggest you to read the full explanation and its very interesting side effects.

As we cannot mix issue on Drupal.org, we will provide a patch for this issue only.

Comments

Pol created an issue. See original summary.

pol’s picture

Status: Active » Needs review
StatusFileSize
new7.88 KB

Here's the patch.

This patch address only this issue.

pol’s picture

StatusFileSize
new8.63 KB

Sorry, the previous patch did not include the newly created files.

Here's the same patch with the previously forgotten files.

The last submitted patch, 2: 2826661.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 3: 2826661.patch, failed testing.

pounard’s picture

Either you remove the 'pattern' test, or you include the nyan cat theme engine to this patch, but as I wrote them one cannot live without the other.

pol’s picture

I will remove the pattern test from here and add it in #2825396: Enable modules to define theme engines.

You're ok with that ?

pounard’s picture

Seems fair enough.

pol’s picture

Status: Needs work » Needs review

I already did it in the patch actually.

If you look closely, I moved the file and function that were in the Nyan Cat engine based theme in test_subtheme, already included in the tests.

Status: Needs review » Needs work

The last submitted patch, 3: 2826661.patch, failed testing.

pounard’s picture

It's very weird, lots of errors seems unrelated. Did you test the patch locally?

pol’s picture

Hi Pounard,

Tests are still running locally... It take ages!
In the meantime I've uploaded the patch, but I still don't get why it fails... investigating.

pounard’s picture

Just run the "theme" group, it is quick, if it works, then it's probably unrelated. It does not have any possible side effects outside of theming, so if the "group" test passes, it should pass globally.

pol’s picture

Status: Needs work » Needs review
StatusFileSize
new8.96 KB

Here's the updated patch. A file was missing, my bad.

pol’s picture

StatusFileSize
new8.76 KB

New patch, removing a function related to the Nyan Cat test theme engine. It will be added by the patch in #2825396: Enable modules to define theme engines.

pounard’s picture

Very nice, it is still missing one test case: right now we are testing with a pattern that is completely different from the theme hook name, hence the #theme => ['first_once', 'second_one'] which is actually an edge case (even more a side effect of the current algorithm) test, I'm going to add a more "normal" use case where the pattern starts with the theme hook name, which actually would be the real life use case.

pounard’s picture

Status: Needs review » Reviewed & tested by the community

In the meantime, the actual patch is RTBC.

If anyone does asks, the line that replaces the strtr() call using preg_quote() was actually not necessary, it just seemed more natural and safer to do this. The original strtr() would allow patterns to use special preg escade characters but in the end, I'm not sure this would be a good thing. In the end I'm even not sure keeping the 'pattern' feature is a good thing.

pounard’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new13.49 KB

I renamed test templates names to have a logical suite. Also added more boundary tests, to make it more robust. Only tests are altered theme.inc code remains unchanged.

Thank you very much Pol, you have more patience than me and I really appreciate that.

pol’s picture

Very nice tests, that was indeed needed.

Merci aussi pour le travail accompli :)

pounard’s picture

Hum my comment about the theme_hook_suggestion is wrong, it is actually documented, but bugguy, I should fix it.

pounard’s picture

StatusFileSize
new13.01 KB

Comments fixed, everything should be good now.

pol’s picture

Status: Needs review » Reviewed & tested by the community

Seems pixel perfect for me.

rene bakx’s picture

This one looks solid to me as well. Would be awesome if this could be committed before the 7.52 window is closed.

stefan.r’s picture

and theme
+  // engines extensions may contain as many dot-separated segment as they with
+  // (e.g. '.tpl.php', '.html.twig', '.phtml', ...).

The grammar here seems a bit wonky (can be fixed on commit)

pounard’s picture

@stefan.r since I'm not a native english speaker, please give us something else we can write in there :)

fabianx’s picture

So far I checked that this seems to be fixed in Drupal 8 already, so that is good.

pounard’s picture

Fabianx yes I did look in Drupal 8 and had the same conclusion, just forgot to mention it here, sorry.

pounard’s picture

About the preg_quote(), did ever even saw a single project using the 'pattern' feature ? By quoting it might quote preg special chars that users might have voluntarily used (although see this actually working would definitely be the second most mystical thing among the universe after the universe itself).

rajab natshah’s picture

Good :)

Are we going to have this in Drupal core?

stefan.r’s picture

Assigned: Unassigned » David_Rothstein
Issue tags: +Drupal bugfix target

@RajabNatshah I think so, the patch looks fine. Assigning to David for further review.

David_Rothstein’s picture

Assigned: David_Rothstein » Unassigned
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.69 KB
new8.86 KB

Perhaps I'm missing something, but I was unable to reproduce this bug in Drupal 7.

For example, see the attached patch (single-filename-extension-do-not-test.patch). Trying that manually and clearing caches, everything with the Seven theme worked fine even though I made it use .php2 as the extension rather than .tpl.php.

The tests included in this issue also don't seem to test that bug - not to say they aren't useful tests in general, but when I ran them without the rest of the patch, they still passed. I am attaching a tests-only version of the patch here just to make sure the same thing happens with the testbot.

As for this part:

-  $regex = '/' . str_replace('.', '\.', $extension) . '$/';
+  $regex = '/' . preg_quote($extension, '/') . '$/';

It definitely looks like a good change, but I think it needs to go in Drupal 8 also.

pounard’s picture

Hum, fun thing, did you test with a git version ? Considering that we applied a series of patch related to theming not so long ago, it might have been fixed along the way. I happened to experience the bug, can't remember the exact circumstances.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.