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.
Task
Use Twig instead of theme function
Remaining
Remove the theme_ functionUpdate all hook_theme definitionsReview patchTest patch- Revisit performance
There is already a twig file for this porpuse. views-view-field.html.twig.
There is a preprocess function for the template already as well: template_preprocess_views_view_field.
Manual testing steps
- Install Drupal 8.
- Create a node of any content type.
- Visit the content administration page at admin/content
- Ensure that the table rows (not the header row) render correctly
Comment | File | Size | Author |
---|---|---|---|
#68 | views_view_field-after.png | 28.03 KB | akalata |
#68 | views_view_field-before.png | 27.64 KB | akalata |
#62 | convert-2348729-62.patch | 2.07 KB | joelpittet |
#59 | convert_2348729_59.patch | 2.73 KB | akalata |
#46 | interdiff2348729-42-46.txt | 1.42 KB | subhojit777 |
Comments
Comment #1
Manuel Garcia CreditAttribution: Manuel Garcia commentedComment #3
Manuel Garcia CreditAttribution: Manuel Garcia commentedComment #4
Manuel Garcia CreditAttribution: Manuel Garcia commentedPrevious patch wasn't applying anymore.
Now that #2226207: Make 'template' the default output option for hook_theme() got in, we no longer need to set the 'template' in hook_theme.
Removed some comments in the template since it'd be now used instead of the theme function.
Comment #6
dawehnerWhen this was tried the first time, the performance was not good enough. Did we changed something here?
Comment #7
Fabianx CreditAttribution: Fabianx commentedLets see how we fare today. The difference might not be that big anymore ...
But why is this failing tests?
Comment #8
Fabianx CreditAttribution: Fabianx commentedComment #9
Manuel Garcia CreditAttribution: Manuel Garcia commentedOK, I've managed to get a promise to help on profiling, if I get the patch to show up green =)
So, I've been trying to fix the tests, and it seems that the errors derive from the twig file (or something before), which adds an empty space after the output. Typical error reads:
I can't figure out why this is happening though... theme_views_view_field($variables) that was being used before the patch doesn't do any processing at all, and the twig file is simple enough. Any pointers¿?
Comment #10
Manuel Garcia CreditAttribution: Manuel Garcia commentedOK, so @Cottser in IRC pointed out to me that it is the new line at EOF that is causing this. This is however a coding standard, so until we figure out what we'll do about this (see #2245901: Trim trailing EOF whitespace from templates), here is an updated patch which modifies the twig file to spit out
{{ output -}}
to get rid of the new line.I've ran the offending tests locally and they all passed, at least we'll be able to profile this patch now.
Comment #11
star-szrLooking at the list of remaining theme functions this would be a great one to get rid of if we can.
Comment #12
davidhernandezPatch still applies.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=54dec59a01be2&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=54dec59a01be2&...
Comment #13
davidhernandezI made a view displaying 100 nodes, 4 fields each. 2 with labels, two without.
Comment #14
davidhernandezMy settings were off. This should be a more accurate run.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=54df8d322535a&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=54df8d322535a&...
Comment #15
akalata CreditAttribution: akalata commentedComment #16
joelpittetThis one looks good from performance. I did a review in #15
Which is about ~25ms extra for rendering the template 400 times, which is about 62.5microseconds per template render. EDIT sigfig error.
There are some weird things that just cancelled themselves out.
I'm adding manual testing to make sure that whitespace modifier is doing what it's supposed to and just need a confirmation there is no line breaks there.
{{ output -}}
Comment #17
Fabianx CreditAttribution: Fabianx commentedWill this template still be used when views field handling uses entity displays? (#1867518: Leverage entityDisplay to provide fast rendering for fields)
Comment #18
joelpittetAs of that patch so far it is still used I think... but thanks for mentioning it, I'm curious how that will pan out.
Comment #19
dawehnerYes it will.
Comment #20
akalata CreditAttribution: akalata commentedComment #21
rpayanmComment #22
dawehnerI'm curious what exactly gain we by replacing a trivial theme function with a template by default ... you are able to override the template, how you want it to be.
Not rendering via the twig template is a performance improvement I think which is worth to keep.
Comment #23
akalata CreditAttribution: akalata commented@dawehner, that might be a better discussion to have in #2348381: [META-0 theme functions left] Convert/refactor core theme functions. Overall, I think we're trying to keep things consistent by removing all theme_ functions.
Comment #25
jdcosta CreditAttribution: jdcosta as a volunteer and commentedI downloaded the patch and applied on my local. I did not see expected results. It needs some work I believe.
I tried it on Drupal 8 beta 10 version on Ubuntu 14.04 with Nginx as running as server.
Attached are some screen shots
Comment #26
Manuel Garcia CreditAttribution: Manuel Garcia as a volunteer commented@jdcosta thanks for the review, can you try again applying the patch against the 8.0.x branch? (you can see git instructions here https://www.drupal.org/project/drupal/git-instructions ).
Also, make sure to run a cache rebuild after applying the patch. (
drush cr
if you are using drush, or clear the cache in the performance page of your drupal installation).Comment #27
star-szrThe pending performance improvements around SafeMarkup (#2295823: Ensure that we don't store excessive lists of safe strings) give me some hope that we could get this performing well enough. That may be false hope but there you go.
Comment #29
daffie CreditAttribution: daffie commentedLooks good to me.
Comment #30
alexpottI don't think that the rtbc in #29 has addressed the performance concerns raised by @dawehner in #22.
Comment #31
star-szrThe patch still applied with offsets but there was a mode change to core/modules/views/views.theme.inc
that snuck in somewhere along the way, this patch reverts the mode change.
And this could use another round of profiling.
No interdiff because the only thing that changed was the mode of the previously mentioned file.
Comment #33
star-szrI looked into the failure in
Drupal\views_ui\Tests\CustomBooleanTest
, it's because custom boolean values like<p>Yay</p>
are being escaped.Comment #34
evilfurryone CreditAttribution: evilfurryone at Wunder commentedComment #35
evilfurryone CreditAttribution: evilfurryone at Wunder commentedComment #36
subhojit777Looking into it
Comment #37
subhojit777Comment #38
joelpittetTwo pickups and drop. Is it really hard reroll? Any hints may help the next person.
Comment #39
subhojit777@joelpittet I was trying to fix it. I checked that the taxonomy links are escaping. Dont have much context about it, so I left it.
Any idea regarding how to fix it OR where to look in code may help the novices.
[OFFTOPIC] I see that for fixing even novice issues you need some context about Drupal 8 - about the archtecture and all (partially at least).
Comment #40
joelpittetThank you @subhojit777 that is helpful feedback that helps sort out why links are escaping.
Regarding off topic, if it becomes not Novice we usually remove the tag. We expected just a simple re-roll. Also for all Novice issues, look up at the parent Meta/Plan issue to see how others were resolved, as they likely have a pattern.
The reason this is happening is because we have auto-escape turned on. So markup that are passed to that output variable that aren't marked safe will be escaped.
Off hand I don't know the right solution ATM but what I plan to do put a breakpoint in the/a preprpocess before this is printed and see where the variable 'output' is being built up and why it may be marked unsafe.
@subhojit777 what would really help is let us know your or a "Steps to reproduce" that we can take to easily see the problem so we don't commit this without a test.
Moving this to Bug report because this needs to work for themes to use with autoescaping and templates as that is a regression since auto-escape was turned on.
Comment #41
joelpittetThis whitespace modifier should do absolutely nothing. But we should actually add a test to ensure this.
Comment #42
joelpittetThis likely needs to be moved to a follow-up for the bug (if it doesn't exist already). But This should fix at least the taxonomy term list links.
Comment #44
subhojit777Thanks @joelpittet I will keep that in mind for future reference. I will work on the last patch and try to fix the issues.
Comment #45
subhojit777Comment #46
subhojit777I was able to fix one test. I got some work to do, will look into it after I get free.
Comment #47
subhojit777Comment #49
subhojit777Comment #50
subhojit777Tests are failing in
CustomBooleanTest.php
. HTML tags are getting escaped in line 103:$output = \Drupal::service('renderer')->renderRoot($output);
$output
is getting escaped even for<p>
tags.Comment #51
subhojit777Will look into it tomorrow.
Comment #52
joelpittet@subhojit777 ok I'm moving these fixes to their own issue and we will postpone this one on those fixes because they will get in faster and they are major/critical.
lol was this the fix? Whoops that shouldn't be there:) Nice find!
Why renderRoot()? This is hardly a root call. Maybe renderPlain() but still how is this fixing things?
Comment #53
joelpittet@subhojit777 could you upload your next patch over in #2560553: Views render pipeline is escaping CustomBooleanTest?
I'm setting up a test to show it's failing in head.
Comment #56
star-szrComment #59
akalata CreditAttribution: akalata commentedQuick reroll. Still expecting tests to fail.
Comment #62
joelpittetJust removing the excess from our existing tests, and going to resolve just the boolean test in the other issue.
Comment #63
akalata CreditAttribution: akalata commentedComment #66
akalata CreditAttribution: akalata commented#62 passes once #2560553-54: Views render pipeline is escaping CustomBooleanTest is applied.
Comment #68
akalata CreditAttribution: akalata commented@joelpittet I know you weren't sure if this was affecting anything - should it be kept in the patch?
Manually tested the following:
Also noting that once this patch gets in, #2118743: Twig debug output does not display all suggestions when an array of theme hooks is passed to #theme will be less easy to test. Oh well.
Comment #69
joelpittetOh I tested the white space modifier and it looks to indeed help with the trailing new line removal for inline fields. I did my test outside Drupal but it should be the same.
Comment #70
joelpittetHere's my latest profiling. About 3.8%.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=5608311397db4&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=5608311397db4&...
Comment #73
davidhernandezaws fails.
Comment #74
RainbowArrayPatch is green again.
Comment #75
star-szrHmm I see contextual_preprocess() is getting called every time, that's unfortunate. I think @joelpittet and I talked briefly about it. It's because template_preprocess() is not called for theme functions. I wonder if there's something we can do to optimize these types of cases, at least for contextual.
Either way I think it's just worth pointing out that contextual_preprocess() accounts for almost 1.4ms here. Not sure if it's worth reprofiling over.
Comment #76
dawehnerRegarding the contextual_preprocess() problem. I'm curious whether we could introduce a flag on render elements which will be require to run contextual_preprocess() on those particular elements in the first place? Given that we have max 19 usages of #contextual_links we could bypass most of the calls in the first place I think.
Could we require callers to maybe also add contextual_preprocess() manually? I guess this could be possible.
Comment #77
webchickHm. I think this could use sign-off from catch as performance topic coordinator. It's worth pointing out that some views, such as issue queue views on d.o, have 50x10 instances of this template, so 3.8% performance slowdown is significant.
Comment #78
lauriiiI'm afraid that views-view-field-html.twig markup is being escaped :(
EDIT: Might be only views markup fields. I'm testing this manually and RTBC'ing back soon if I can make sure that is correct.
Comment #79
lauriiiBug exists only for the custom text views field. Back to RTBC.
Comment #80
catchWe've committed to using templates everywhere, so places like this (and actual field templates) we need to take the performance hit to gain the security/consistency.
I did open #2580263: Find a way to not run contextual_preprocess() on every template to look at contextual_preprocess().
Committed/pushed to 8.0.x, thanks!