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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers created an issue. See original summary.

Wim Leers’s picture

Wim Leers’s picture

We shouldn't have to do this when inheriting templates, but it's the current standard, so alas…

The last submitted patch, 2: remove_template_preprocess_image-2694179-2.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 3: remove_template_preprocess_image-2694179-3.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
13.4 KB
3.56 KB

I 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).

Wim Leers’s picture

  1. +++ b/core/themes/stable/stable.theme
    @@ -23,3 +23,67 @@ function stable_preprocess_links(&$variables) {
     }
    +
    +
    +/**
    

    One newline too many.

  2. +++ b/core/themes/stable/templates/field/image.html.twig
    @@ -4,10 +4,28 @@
    - *
    - * @see template_preprocess_image()
    

    Should be reverted, and point to the copy of that function in Stable.

Wim Leers’s picture

This should fix the other fails.

Wim Leers’s picture

The last submitted patch, 6: remove_template_preprocess_image-2694179-6.patch, failed testing.

The last submitted patch, 8: remove_template_preprocess_image-2694179-8.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 9: remove_template_preprocess_image-2694179-9.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
13.47 KB
775 bytes

One silly mistake.

attiks’s picture

Only thing I found is a small comment nitpick.

+++ b/core/modules/system/templates/image.html.twig
@@ -4,12 +4,63 @@
+ * - srcset: Array of multiple URIs and sizes/multipliers.
+ * - sizes: The sizes attribute for viewport-based selection of images.

+++ b/core/themes/classy/templates/field/image.html.twig
@@ -1,13 +1,32 @@
+ * - srcset: Array of multiple URIs and sizes/multipliers.
+ * - sizes: The sizes attribute for viewport-based selection of images.

+++ b/core/themes/stable/stable.theme
@@ -23,3 +23,66 @@ function stable_preprocess_links(&$variables) {
+ *   - srcset: Array of multiple URIs and sizes/multipliers.
+ *   - sizes: The sizes attribute for viewport-based selection of images.

+++ b/core/themes/stable/templates/field/image.html.twig
@@ -4,10 +4,30 @@
+ * - srcset: Array of multiple URIs and sizes/multipliers.
+ * - sizes: The sizes attribute for viewport-based selection of images.

add (optional) to be consistent, see stylename explanation.

Wim Leers’s picture

+1 for that consistency (that is missing in HEAD).

Wim Leers’s picture

Status: Needs review » Needs work

The last submitted patch, 16: remove_template_preprocess_image-2694179-16.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review

Re-tested because the fails are in MigrateForumConfigsTest , which make no sense. Also, #16 only changed comments, so by definition this fail is illogical.

davidhernandez’s picture

Status: Needs review » Needs work
--- a/core/themes/classy/templates/field/image.html.twig
+++ b/core/themes/classy/templates/field/image.html.twig
@@ -1,13 +1,33 @@

@@ -1,13 +1,33 @@
+{% extends "@system/image.html.twig" %}

We 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.

lauriii’s picture

Agreed 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

Wim Leers’s picture

We can't do this. Even if we can guarantee the same result now, it would have Classy extending off of an unstable template.

First 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?

We have to move the preprocess and template to Stable to ensure that any custom template overrides doesn't break.

This patch already does that.

lauriii’s picture

Nope, 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.

Wim Leers’s picture

Oh, 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 on stable_preprocess_image())

lauriii’s picture

Yes, but Classy extends Stable so there should be no need to repeat anything in Classy and most likely we can just leave it untouched

star-szr’s picture

What I would love to see here is an issue summary explaining the motivation, otherwise I assume "because we can" :)

Wim Leers’s picture

Issue summary: View changes
Status: Needs work » Needs review

#24: Alright, done. I saw a slightly more elegant way, so I went with it.

#25: Added a motivation. Because simplicity: TX matters.

Wim Leers’s picture

And I forgot to attach the updated patch. Oops.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Looks good! I hope we'd have unstable classy but this is good for now!

catch’s picture

Assigned: Unassigned » star-szr

Making 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.

star-szr’s picture

Status: Reviewed & tested by the community » Needs review

I'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:

+++ b/core/modules/system/templates/image.html.twig
@@ -4,12 +4,63 @@
+  {% if _context[attribute_name] is defined and attributes[attribute_name] is empty %}
+    {% set attributes = attributes.setAttribute(attribute_name, _context[attribute_name]) %}
+  {% endif %}

Specifically the use of _context and is defined/is empty, but it's also inside a loop :)


Finally, just a general comment:

+++ b/core/modules/system/templates/image.html.twig
@@ -4,12 +4,63 @@
+    {% if source.width is defined %}
...
+    {% elseif source.multiplier is defined %}

Do we need is defined for these?

star-szr’s picture

Assigned: star-szr » Unassigned
Wim Leers’s picture

The 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. map width to attributes.width without checking if attributes.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 of isset() (to check if an array key exists), so, yes.

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?

Self-containedness is what this brings. One less reason to find your way through the mess maze of PHP where, somewhere, there is some function that affects what you get in your template. For example, for the srcset 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).

dawehner’s picture

In 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.

Wim Leers’s picture

Assigned: Unassigned » catch

That's an excellent remark. I wonder what @Cottser, @catch and @alexpott think about that.

catch’s picture

I'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?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Appears there is still some discussion to be had. Once a decision is made maybe an updated IS is needed.

catch’s picture

Assigned: catch » Unassigned

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.