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_container
Twig Templates Modified
container.html.twig
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | interdiff-2329759-6-9.txt | 691 bytes | sutharsan |
| #9 | drupal-container-classes-2329759-9.patch | 1.55 KB | sutharsan |
| #6 | drupal-container-classes-2329759-6.patch | 1.63 KB | sutharsan |
Comments
Comment #1
davidhernandezComment #2
sutharsan commentedComment #3
sutharsan commentedtemplate_preprocess_containeradds one static class. No comments to change in either the preprocess or the twig template.Comment #4
star-szrThanks @Sutharsan! I think we need to create a variable in preprocess to use in the Twig template to set this class conditionally. Otherwise the form-wrapper class will be added unnecessarily.
Comment #6
sutharsan commentedHmm, did I really overlook that :/
Not 100% sure about the name 'has_parent'. But it matches with FAPI #array_parents
Comment #7
rainbowarrayThe method we've preferred for setting conditional classes is a ternary operator in a set classes block at the top. So something like this:
has_parent ? 'form-wrapper'
You can look at the meta for an example of how this is done.
The advantage of this approach is that it isolates the variation to just the class name. Otherwise if somebody wants to change the HTML, they have to change it in two places.
Thanks for the patches!
Comment #9
sutharsan commented#7 comment included.
However, .... this will fail some tests because
<div{{ attributes.addClass() }}>results in<div class="">. This is because addClass assumes existing classes.Comment #10
sutharsan commentedI created #2330731: Attribute::addClass() adds empty class to fix the empty class problem.
Comment #12
star-szrGreat, this looks pretty good to go after that gets committed. The only thing is according to our guidelines in #2322163: [meta] Consensus Banana Phase 1, move CSS classes from preprocess to twig templates. since this is using a ternary it should be in a set tag, but we can discuss that as well.
Comment #13
davidhernandez#2330731: Attribute::addClass() adds empty class has been committed.
Comment #15
star-szrComment #17
star-szrFor anyone following along at home, we are now blocked on #2336355: Refactor Attribute rendering so that class="" is not printed.
Comment #18
davidhernandez#2336355: Refactor Attribute rendering so that class="" is not printed is in.
Comment #20
star-szrGood to go now.
Comment #21
catchCommitted/pushed to 8.0.x, thanks!
Comment #24
Leagnus commentedHow can i get views name (or any other views attributes) in container.html.twig
to set individual class for its wrapper? i'm using bootstrap theme.
Should i initially set a var in a theme_preprocess_page(), and then check its value in the template file?
No: i should use hook_preprocess_container()