Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It's difficult to extend the image.html.twig
template in small ways, because every implementation of the template duplicates everything, rather than extending. But it is particularly difficult because the majority of the logic still happens in a preprocess function.
Proposed resolution
Remove template_preprocess_image()
, move its logic into the Twig template.
But keep BC for the Stable & Classy themes, so duplicate that preprocess function to stable.theme
.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#27 | remove_template_preprocess_image-2694179-26.patch | 13.51 KB | Wim Leers |
#22 | Blank_Flowchart_-_New_Page__2_.png | 32.92 KB | lauriii |
#22 | theme-overview.png | 62.45 KB | lauriii |
Comments
Comment #2
Wim LeersComment #3
Wim LeersWe shouldn't have to do this when inheriting templates, but it's the current standard, so alas…
Comment #6
Wim LeersI guess this violates the stability of Stable. So let's keep the preprocess function for Stable and not change the template at all (except for fixing the horribly incomplete docs).
Comment #7
Wim LeersOne newline too many.
Should be reverted, and point to the copy of that function in Stable.
Comment #8
Wim LeersThis should fix the other fails.
Comment #9
Wim LeersFixes nits in #7.
Comment #13
Wim LeersOne silly mistake.
Comment #14
attiks CreditAttribution: attiks at Attiks commentedOnly thing I found is a small comment nitpick.
add (optional) to be consistent, see stylename explanation.
Comment #15
Wim Leers+1 for that consistency (that is missing in HEAD).
Comment #16
Wim LeersAddressed #14.
Comment #18
Wim LeersRe-tested because the fails are in
MigrateForumConfigsTest
, which make no sense. Also, #16 only changed comments, so by definition this fail is illogical.Comment #19
davidhernandezWe can't do this. Even if we can guarantee the same result now, it would have Classy extending off of an unstable template.
Moving the preprocess to Stable should be fine, as well as updating the System template.
Comment #20
lauriiiAgreed with #19. We have to move the preprocess and template to Stable to ensure that any custom template overrides doesn't break. Then if we want to, we can also implement the reverting change in Bartik and Seven if we want to try it out in an actual theme unless we fix #2659890: [Policy] [Plan] Drupal 9 and 10 markup and CSS backwards compatibility
Comment #21
Wim LeersFirst I want to understand why the default template provided by the
system
module is "unstable"? Can you explain that? Only system module can change it. That module lives in core. So as long as we don't change that template, things remain the same. Furthermore, Classy can change things (and be unstable), it's only Stable that must be stable?This patch already does that.
Comment #22
lauriiiNope, Classy and Stable are stable. That's why Stable is the default base theme that themes are extending unless they explicitly specify not to extend it.
Some pictures to be explicit:
In the picture dark blue themes are stable and light blue ones are unstable.
Flowchart how people are evaluating which theme they want to extend.
Comment #23
Wim LeersOh, hah, I didn't know that. #Drupal8noob Sorry about that!
So I need to repeat for Classy what I did for the Stable theme? (i.e. NOT do
{% extends "@system/image.html.twig" %}
, but instead rely onstable_preprocess_image()
)Comment #24
lauriiiYes, but Classy extends Stable so there should be no need to repeat anything in Classy and most likely we can just leave it untouched
Comment #25
star-szrWhat I would love to see here is an issue summary explaining the motivation, otherwise I assume "because we can" :)
Comment #26
Wim Leers#24: Alright, done. I saw a slightly more elegant way, so I went with it.
#25: Added a motivation. Because simplicity: TX matters.
Comment #27
Wim LeersAnd I forgot to attach the updated patch. Oops.
Comment #28
lauriiiLooks good! I hope we'd have unstable classy but this is good for now!
Comment #29
catchMaking sure Cottser gets a chance to take a look at this.
The template change is a bit o__O for me, but also getting rid of preprocess has been on my list for years now, so +1 from me in principle.
Comment #30
star-szrI'm still not totally sure about this - what are we gaining in a practical way other than one less template_preprocess_* function? Do we have a use case for users modifying this logic directly in the template, specifically something that couldn't be done by manipulating the attributes in the template?
I'm not opposed to this in principle (I think it actually does match with the principles that were set out, specifically "Don't dumb it down") but I think we should discuss this one a bit more because to my knowledge we don't have anything quite this complex in core:
Specifically the use of _context and is defined/is empty, but it's also inside a loop :)
Finally, just a general comment:
Do we need
is defined
for these?Comment #31
star-szrComment #32
Wim LeersThe use of
_context
is for BC. I agree that it's needlessly complex, and should be simplified. But simplifying it is IMO out of scope here, because simplifying it means some of the automagic behaviors go away. If everybody wants to have this simplified (i.e. mapwidth
toattributes.width
without checking ifattributes.width
is already defined), then I'd be happy to do so. But I'm not sure it's worth the BC break.RE:
is defined
: that's the equivalent ofisset()
(to check if an array key exists), so, yes.Self-containedness is what this brings. One less reason to find your way through the
messmaze of PHP where, somewhere, there is some function that affects what you get in your template. For example, for thesrcset
variable that you can pass, there is quite a bit of processing. Currently, as a themer, it's hard to understand how that is processed. This makes that more explicit.It is this self-containedness that is crucial for #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering). i.e., this is one small step on the way to #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering).
Comment #33
dawehnerIn general we should think a bit more about BC. Preprocess functions aren't just a thing for themes, but they are something also for modules.
For example there is the Lazyloader module which used to implement its logic (replace the used image attributes) inside another
hook_preprocess_image()
method. If we move the logic from the preprocess function into a template, this could be sort of a blocker/problem for those modules. Whether modules should be able to provide that kind of functionality is a different question, but I want to rise the point, that its not risk free.https://www.drupal.org/core/d8-bc-policy doesn't talk yet about preprocess functions.
Comment #34
Wim LeersThat's an excellent remark. I wonder what @Cottser, @catch and @alexpott think about that.
Comment #35
catchI'd put preprocess in the same category as alter. i.e. no guarantees about what you get, or about whether what you add to it eventually gets used - might need adding to the doc though.
I think there's a question about how much logic we should be moving to templates, vs. how much logic we could potentially do earlier than preprocess - at least with entity rendering, a lot of things that used to be done in preprocess can be done in hook_entity_view()/alter() and friends which aren't going anywhere at the moment. i.e. do we really need to check
if defined
in the template or could we initialize earlier than the template instead?Comment #47
catchComment #50
smustgrave CreditAttribution: smustgrave at Mobomo commentedAppears there is still some discussion to be had. Once a decision is made maybe an updated IS is needed.
Comment #51
catch