Move classes out of the preprocess functions and into the Twig templates. Use the addClass() attribute method to add classes in the template. Use the clean_class filter to filter class names, if necessary. Maintain all existing functionality and ensure all existing class names are still in the markup, even ones that are inherited.
See the following issues for more detailed examples:
#2217731: Move field classes out of preprocess and into templates
#2254153: Move node classes out of preprocess and into templates
See this change record for information about using the addClass() method:
https://www.drupal.org/node/2315471
See this change record for more information about the phase 1 process of moving class from preprocess to templates:
https://www.drupal.org/node/2325067
Preprocess Functions Modified
template_preprocess_views_view
template_preprocess_views_view_fields
template_preprocess_views_view_summary
template_preprocess_views_view_summary_unformatted
template_preprocess_views_view_table
template_preprocess_views_view_grid
template_preprocess_views_view_unformatted
template_preprocess_views_view_list
template_preprocess_views_mini_pager
Twig Templates Modified
views-view.html.twig
views-view-fields.html.twig
views-view-summary.html.twig
views-view-summary-unformatted.html.twig
views-view-table.html.twig
views-view-grid.html.twig
views-view-unformatted.html.twig
views-view-list.html.twig
views-mini-pager.html.twig
Beta phase evaluation
Issue category | Task because it improves the Themer Experience. |
---|---|
Issue priority | Normal. |
Unfrozen changes | Unfrozen because it changes markup. |
Prioritized changes | The main goal of this issue is usability of the new Twig theming layer, and since it adjusts markup it must be completed prior to RC. |
Disruption | This issue is a prioritized change (theme improvements) as per https://www.drupal.org/core/beta-changes and it's benefits outweigh any disruption. |
Comment | File | Size | Author |
---|---|---|---|
#80 | interdiff-79to80.txt | 2.88 KB | davidhernandez |
#80 | move_views_classes_from-2329917-80.patch | 18.76 KB | davidhernandez |
#79 | move_views_classes_from-2329917-79.patch | 19.09 KB | davidhernandez |
#73 | interdiff.txt | 1.38 KB | Manuel Garcia |
#73 | move_views_classes_from-2329917-73.patch | 18.77 KB | Manuel Garcia |
Comments
Comment #1
davidhernandezComment #2
lauriiiComment #3
lauriiiHere's my progression on this one. There's still a lot to do so I'm just sending this to test bot.
Comment #4
lauriiiUnassigning myself. I will continue working with this one but anyone who feels like grabbing this, go ahead!
Comment #6
lauriiiThere's still at least
template_preprocess_views_mini_pager
which I didn't do because of #2318341: Views mini pager markupComment #8
lauriiiThis should fix the tests
Comment #9
lauriiiComment #10
lauriiiRemoved debugging line
Comment #11
star-szrI think this is looking really good, thank you @lauriii!
Minor but can we maintain the indent here please?
Comment #12
lauriiiComment #13
lauriiiNow the indentations should be good!
Comment #14
mikl CreditAttribution: mikl commentedReviewed – good work, and the indentation is now correct.
Comment #15
dawehnerJust curious whether this was discussed properly, which I don't see here, sadly.
So previously we had one place for this logic, now there are two. How is that a good idea?
Same, but different.
Same question here.
Write this shit in one line, please
so wait what? Why do we not add this into the template?
Comment #16
tim.plunkettComment #17
lauriiiComment #19
lauriiiFixing tests
Comment #21
lauriiididnt remember that row classes can have tokens inside
Comment #22
lauriii#15.1 and #15.2 should still be checked
Comment #23
lauriiiTested few different options to clean up #15.1 and #15.2 and didn't find anything dramatically cleaner that would fit into this issues scope.
Comment #24
lauriiiComment #26
LewisNymanReroll
Comment #28
rteijeiro CreditAttribution: rteijeiro commentedUnless testbot doesn't agree, this looks RTBC.
Comment #30
lauriiiRerolled
Comment #32
lauriiiRerolled and fixed some documentation
Comment #33
lauriiiAdded documentation to responsive variable
Comment #34
jamesquinton CreditAttribution: jamesquinton commentedIn views-view.html.twig, if the id is being passed to the clean_class filter on line 43, should it not also be passed elsewhere? Is this actually needed here?
Still some classes being added in preprocess functions, e.g:
template_preprocess_views_view_table()
template_preprocess_views_view_list()
Comment #35
Anonymous (not verified) CreditAttribution: Anonymous commentedMoved table view classes to twig template file.
Comment #36
Anonymous (not verified) CreditAttribution: Anonymous commentedOops, forgot to remove 2 lines commented out from the old code in the previous patch file. This is the correct one.
Comment #37
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #38
lauriiiI think we have to move a bit backwards with this. We cannot move the Views UI html class functionality to templates because it would break if someone removes the variables from the templates. E.g moving the classes to the classy would break it. Different question is that if we need it anymore in Views UI that is in core but it shouldn't be discussed here.
Comment #39
Anonymous (not verified) CreditAttribution: Anonymous commentedYes, it would be much better for front-end developers if we are able to stream-line this process. E.g having all class definitions only in the template file and give themers complete control over what go in there and keep the Views UI to pretty much bare-bone. @lauriii, has there been any discussion about this anywhere? Would be nice to follow up on this.
Comment #40
lauriiiI haven't seen any discussion on d.o regarding such matter but we've had some unofficial discussions about that. I guess we are too late to get that into D8.
Anyhow I moved table default classes to template and added some more documentation for missing parts.
Comment #41
LewisNymanI diffed the html output in HEAD and with the patch and the only difference is the order of the attributes and classes, which is fine. It looks like the php has already been reviewed. RTBC!
Comment #42
alexpottSo are there no answers to #15.1 and #15.2?
Comment #43
lauriiiIf I remember correctly we discussed this with dawehner in Amsterdam and came into consensus that refactoring that isn't in the scope of this issue. We could instead create follow-up to fix it.
Comment #44
alexpottThis kind of bothers me. We're in a loop - one row is probably going to be active and we're creating and adding an empty class to all the other rows. How about doing something like
instead. We have lots of stuff occurring in loops here and I think this kind of optimisation could be used elsewhere in the patch.
Also this issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary. Obviously as part of the banana consensus it can go in but it's nice to have the justification in a standard way on every issue.
Comment #45
Anonymous (not verified) CreditAttribution: Anonymous commentedI agree that @alexpott suggestion is a more optimised option. But from what I have seen so far, the classes are always defined in one place in the template file. for example:
Having to defined the active class differently would - in a way - introduces irregularity in the template structure. Also when the template is overridden, themers might want to add extra classes besides the active class (custom-class-1, custom-class-2, etc...) so (in a way) we are not always necessarily calling the addClass function on empty classes array. It might not be the most optimised, but I believe is the cleanest way.
On the side note, in order to test changes on the views-view-summary-unformatted.html.twig template, you will need to add a contextual filter to the view block with the option Display a summary and show the block on every page. Active class for the view's link will then be checked against the currently viewed url. Took me a while to figure this out so I hope it might be useful for someone else as well.
Comment #46
lauriiiFixes #44
Comment #48
Dragan Eror CreditAttribution: Dragan Eror commentedOne question here: Shouldn't we change 'active' class to 'is-active'?
There is already issue to change all classes in core #2031641: Change active class to is-active
Comment #49
davidhernandezadding beta evaluation text.
Comment #50
lauriii@Dragan Eror: I don't think its in the scope of this issue
Comment #51
lauriiiIt's not possible to modify attributes object there because its protected. Tried something Cottser suggested to try.
Comment #53
lauriiiIgnore latest patch :P
Comment #54
joelpittet@lauriii I really like the clean-up from the last patch, could you do that here too?
Comment #55
LewisNymanSetting to needs work based on joel's comments
Comment #56
lauriiiYeah that's a good catch
Comment #58
akalata CreditAttribution: akalata commentedRerolled to include small coding style changes where the new variables are documented (to follow correct style from https://www.drupal.org/node/1354#lists).
Reviewed for:
Consistent changes to templates
Consistent changes to summary templates
Consistent changes to preprocess functions
Summary of open concerns addressed
Issue raised in #44, reminded in #54, resolved in #56.
Raised in #15.1/#15.2, rephrased in #45, declared as out-of-scope in #43 (follow-up issue recommended?)
Reviewing against list of functions/templates intended to be modified
attributes
declarations, butb. template_preprocess_views_view_fields() has
$attributes['class']
.attributes
declarations, andb. template_preprocess_views_view_list() has
$variables['attributes']['class']
.Comment #59
akalata CreditAttribution: akalata commentedDidn't mean to remove sprint tag. Setting needs work for feedback on 1-3.
Comment #62
rootworkComment #63
lauriiiI think the patch didn't support multifield columns. I tried to fix that.
Comment #64
Manuel Garcia CreditAttribution: Manuel Garcia commentedCompared markup being printed out before vs after the patch, on these situations (with generated content):
OK = identical markup
laurii's change on #63 to previous patch does the trick for multifield columns.
However, I'm setting back to needs work because we still have to work on missing parts of this patch, quoting from #58:
attributes
declarations, butb. template_preprocess_views_view_fields() has
$attributes['class']
.attributes
declarations, andb. template_preprocess_views_view_list() has
$variables['attributes']['class']
.Comment #65
davidhernandez#64 1 - We might not be able to do anything about this. This looks like the view ui stuff getting integrated. Am I reading that right? 3 - This is ok. The classes will get removed when we get the templates into Classy, but not all classes have to be added through an attribute.
Comment #66
Manuel Garcia CreditAttribution: Manuel Garcia commentedAbout 1:
The preprocess function in this case is actually building the markup that the user has configured through the admin UI. I agree with @davidhernandez, unless we want to do all the processing on the twig file, we should just leave it be there. The $attributes['class'] array gets processed directly in there
$attributes = new Attribute($attributes);
to build up the markup string for the html tag, as configured by the user.Only thing that we could do to improve themers' lives would be to think about the
element_default_classes
: 'field-content', 'views-field' and 'views-label'. But then again you can swap these out by de-selecting the add default classes options. Not sure it'd be worth the effort.And I think the same case applies for item 2:
The classes here are also coming from Views UI user configuration.
Comment #67
mortendk CreditAttribution: mortendk commentedWe will have things that comes from the UI, thats "unfortunately" But its not our role to take that discussion + that ship have sailed & its a timesuck we dont wanna get into.
Make sure that the UI can add classes/whatever as a user wanna add in through the ui its a feature (not a bug...)
as david says its ok + its a huge step forward from what we have now.
views mini-pager should have all those classes, so its the same structure as pager.html.twig, in the "move templates to classy & cleanup core for not needed classes" follow up issue, we will take care of cleaning classes.
Renaming of classes & what they should be called will first happen when templates are in classy.
Comment #68
Manuel Garcia CreditAttribution: Manuel Garcia commentedAlright then all is good to go now that we have cleared out the last 3 outstanding points in #65, #66 and #67.
The patch still applies fine, so RTBC.
Comment #69
damiankloip CreditAttribution: damiankloip commentedWhat happens when these template variables are not set? E.g. col_class_default is FALSE.
Comment #70
dawehnerThis looks great, compared the templates with the corresponding preprocess functions ... and they look alright.
typo
Afaik you can just use
!empty($options['default_row_class'])
to simplify this row of code.Comment #71
alexpottFor #70 and it'd be nice to get answer to #69 - I think the answer is twig does not care.
Comment #72
Manuel Garcia CreditAttribution: Manuel Garcia commented@damiankloip (#69) - The markup outputted in the case you describe is exactly the same before applying this patch than after.
You basically get this as a wrapper for the item:
<div style="width: 25%;">
(for 4 columns).Without the patch:
With the patch applied (and cache rebuilt):
Working on #70
Comment #73
Manuel Garcia CreditAttribution: Manuel Garcia commentedFixing what was mentioned on #70, interdiff against #63.
Comment #74
davidhernandezFor #69 - we are not using Twig in strict mode so, yes, Twig does not care.
Comment #75
davidhernandezSetting back to RTBC based on the interdiff answering #70.
Comment #76
joelpittet@dawehner mentioned in #15 this hunk. Has anybody tested this "horizontal"?
missing clearfix for horizontal to function?
Comment #77
joelpittetSorry for wet blanketing the RTBC, this patch looks great so far!
Comment #78
davidhernandez#76, the col/row and clearfix classes are showing up correctly for me. I manually tested a view with grid display. The clearfix comes out in the right spots when vertical or horizontal.
I'm leaving needs work for now, since I agree views-view-grid might need attention. The "set classes" are mostly being duplicated in two places, but we can probably streamline it.
Comment #79
davidhernandezThis needed a reroll after a commit that just happened. The only difference is I fixed some indenting in the views-view-grid template.
Regarding that template, I'm now not sure how to easily refactor it. We're actually dealing with multiple conditions, because of the col/row options from the view, and also the loop index. I'll play with it, but someone else feel free, or maybe we are good the way it is.
Comment #80
davidhernandezI tried refactoring the template, pulling the ifs out and condensing things a bit.
Comment #81
Manuel Garcia CreditAttribution: Manuel Garcia commentedI like it @davidhernandez, it's a lot more clear to read, and work with.
I tested it manually and the classes are still added properly for vertical & horizontal view displays.
If we were to make it even easier, I think we could perhaps split the file into two, one for vertical and another for horizontal:
Although that would mean duplicating code a bit, so I'm not convinced with the idea.
What do you think? Is this clear enough for themers, or should we try to go the extra mile?
Comment #82
davidhernandezI wouldn't split them. That seems like overkill, and you would have to override both of them when you wanted to make changes in your theme. I think the template could be simplified even further, since it is really just about one series of divs wrapping another, but that is out of scope for this issue. Lets just get the preprocess changes in.
Comment #83
mortendk CreditAttribution: mortendk commenteddont split em - thats overkill.
Comment #84
Manuel Garcia CreditAttribution: Manuel Garcia commented@davidhernandez - all good points
OK, let's get this one in!
Comment #85
alexpottCommitted 5f05852 and pushed to 8.0.x. Thanks!
Thank you for adding the beta evaluation to the issue summary.
Comment #87
davidhernandezRejoice!
Comment #88
mortendk CreditAttribution: mortendk commentedso this was banana phase 1 ?
*dance*
Comment #89
Manuel Garcia CreditAttribution: Manuel Garcia commentedAwesome!!
Next: