#788900: Deprecate and remove usages of arg() reminded me that we still generate theme and template suggestions based on path, for example page--node--1.tpl.php
The problem
Now that most code in core is based on routes, someone could change /node/% to /content/% and your theme would break. This makes those suggestions impossible to use in contrib themes since they can't work generically.
Additionally HTML head is one of the least-cacheable parts of the page in 8.x, and per-path suggestions will make it harder to cache that HTML.
Also these suggestions are a bit broken in D8 in the first place, they include a suggestion with a percent character:
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'html' -->
<!-- FILE NAME SUGGESTIONS:
* html--node--2.html.twig
* html--node--%.html.twig
* html--node.html.twig
x html.html.twig
--><!-- THEME DEBUG -->
<!-- THEME HOOK: 'page' -->
<!-- FILE NAME SUGGESTIONS:
* page--node--2.html.twig
* page--node--%.html.twig
* page--node.html.twig
x page.html.twig
-->Proposed solution
Remove the automatic theme suggestions based on path parts.
Why this should be an RC target
1. This is code removal only.
2. In general, contrib themes never use the per-page suggestions (and if they do they're fragile), they're sometimes used by custom themes (and often incorrectly when block configuration, page manager or similar would be a better choice). So the disruption in terms of number of themes using the feature should be low.
3. New theme developers sometimes use this 'by mistake' instead of the better options in #2, which can lead to custom themes with 10-15 page templates all with minro variations, this is one of the worst maintenance issues with custom themes in the wild.
4. Also themes can add back the suggestions by copying the removed code to template.php.
5. While templates using the suggestions won't be picked up, there's no other disruption to actual sites from the change in terms of errors etc. even if they're using the feature, which 99.9% won't be.
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | remove-current-path-2275487-26.patch | 1.1 KB | Arnion |
| #11 | remove_current_path-2275487-11.patch | 1.1 KB | davidhernandez |
| #10 | remove_current_path-2275487-10.patch | 1.09 KB | joelpittet |
Comments
Comment #1
Crell commentedI can't say I've used that functionality since Drupal 4.7 or so. Routes, view modes, etc. are far more robust. I'm +1 on removing the path-based suggestions.
Comment #2
andypostThe case in current project: I got a "/home" page that uses different layouts depending on user role,
and at the same time I need one of the same
page--home-single.twig.htmlfor other pages.So
hook_preprocess_page()is checking route object and current user.Once we remove path based suggestions we need to introduce a clean way to use route name
Comment #3
davidhernandezHow much functionality would we lose? Would we lose page--front?
Comment #4
dawehner@davidhernandez
We talk about this lines of code:
so this does not remove the
page--frontsuggestion.Comment #5
davidhernandezWe are currently talking about this on the Twig group call, and no one seems to care if it goes. :)
Comment #6
mortendk commentedYeah well - let me have an opinion on this;
Using a nid for a template is a common "mistake" that people do only cause its there, and D7 is such a pita to theme.
its an epic pita to take over a theme where theres page-nid-1.tpl, page-nid-9874.tpl, page-taxonmi-999. tpl - So killing the nodeid/termid - im fine with that
What would be sad was to not more have the path, as it do have some value (page-sectionname.html.twig / page-front.html.twig), cause thats a very common usecase.
else we would have to write that stuff ourself in foo.theme (so that would need some documentation + handholding)
Comment #7
andypostchange record should describe a way to use page templates, #2 is not answered
Comment #8
catch@Morten so you mean keep the first part of the path like 'node', 'user', 'admin'?
That would still help with the cacheability part of this quite a lot.
Comment #9
mortendk commented@catch yup at least keeping the first part :)
p
age--section.html.twigwould be a common usecase where a theme would probably do an{% extend page.html.twig %}and then modify it that way.i havent looked yet in how complicated it is to add new template suggestion to the theme in .theme, if its dead easy, this could just be a documentation issue
Comment #10
joelpittetHere is a patch to start the ball rolling on this.
Comment #11
davidhernandezI think we should decide whether we're going to do this, and to what degree.
#11 needed rerolling.
Comment #12
catchTagging.
Comment #13
joelpittetMissed the deadline on this one, bumping to 8.1.x
Comment #14
catchWe missed the deadline but AFAIK this already doesn't work in head. So tagging for triage during rc
Comment #16
xjmTo have committers consider it for inclusion in RC, we should add a statement to the issue summary of why we need to make this change during RC, including what happens if we do not make the change and what any disruptions from it are. You can add a section
<h3>Why this should be an RC target</h3>to the summary.Comment #17
catchFleshed out the issue summary a bit.
Comment #18
catchComment #19
frob#11 Worked for me.
Comment #20
catchComment #21
alexpottReading the issue summary and #6 makes me think this is a very good thing to do.
Is there anyway we can detect such templates and warn existing sites that they are now broken?
Comment #22
alexpottMarking needs work for getting the CR written.
Comment #23
xjmComment #24
berdirI think this is an API change at this point, so no longer an option for 8.x? We could deprecate it or something but given that no IDE will show you that, I'm not sure how useful that really is?
Comment #25
catchThe chance of a base theme or contributed theme using this is very slim.
That leaves custom themes for specific sites mostly. For those I think there are options:
- put it behind a setting, enabled for new sites but not existing ones
- put it in a new optional module, enabled for new sites but not existing ones
Or enabled for all sites by default then individual sites could disable it.
Since we have hook_theme_suggestions_alter(), there's no actual guarantee that this is available to all themes on all sites all the time already.
Comment #26
Arnion commentedPatch rerolled.
Comment #27
Arnion commentedComment #28
lokapujyaMaybe an exception could be made to allow a BC break. But still Needs change record.
Comment #29
davidhernandezIs that backwards? Shouldn't it be enabled for existing sites but not new ones?
Regarding the BC module idea, has this been done yet for anything else? What is the plan for that exactly? Will there be multiple BC modules for different fixes, or one that covers everything as core progresses?
Comment #30
catchWe haven't so far needed a backwards compatibility layer in the same way that this issue might.
Generally changes we're making are either covered by the @internal policy (so we don't provide bc since it's not part of the API), or we just add @deprecated notices.
#2042165: Add a 'deprecated' module that includes deprecated functions only when enabled was the issue I first proposed the bc module. If we decide that 1. it's worth doing this issue at all 2. we need to provide bc, then this would be the first time it'd genuinely come up.
Another way might be to move the preprocess to 'Stable' - then all themes get it except those that have explicitly opted out of backwards compatibility, but that means slightly broadening the scope of stable then.
Comment #31
davidhernandezYeah, I thought about Stable but I don't like that idea. It essentially makes it permanent for most people since we aren't encouraging people to not use Stable. Being in preprocess you couldn't even undo it, could you?
Comment #32
star-szrThey could remove the suggestions from Stable in their own theme suggestion alter hook.
Looking at some of the basic suggestions that this code is creating out of the box (added to the issue summary) I don't think it's a problem to remove the
node--2andnode--%(ha) but I worry that a non-trivial amount of folks will be using page--node/html--node suggestions. If we do add BC to Stable maybe we can limit it to the first path component?Comment #47
smustgrave commentedThank you for creating this issue to improve Drupal.
We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #48
berdirLets keep this for now.
We're close to add support for theme hook suggestions. That said, not sure this is worth the trouble. This is discouraged but there are going to be so, so many such templates out there