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_links
Twig Templates Modified
links.html.twig
Comment | File | Size | Author |
---|---|---|---|
#30 | move_links_classes_from-2329763-30.patch | 4.31 KB | lauriii |
#30 | interdiff-2329763-26-30.txt | 1.14 KB | lauriii |
Comments
Comment #1
davidhernandezComment #2
Sutharsan CreditAttribution: Sutharsan commentedComment #3
Sutharsan CreditAttribution: Sutharsan commentedLink keys used as classes moved to twig template.
I did not touch the header class code (below) in template_preprocess_links , as I think it will be removed in #2285493: Remove deprecated 'class' property from #theme 'links' and #theme 'menu_tree' heading arrays.
Comment #4
RainbowArrayI'm not clear what this is doing?
Comment #6
Sutharsan CreditAttribution: Sutharsan commentedIt just moves
$li_attributes = array();
approx. 15 lines down. This is the resulting code:[edit: move verbose explanation]
Comment #7
Sutharsan CreditAttribution: Sutharsan commentedBack to needs review.
Comment #8
star-szrMade the necessary test updates, this should be green now.
Comment #9
star-szrThis is looking good to me now, needs more reviews though.
Comment #10
lauriiiTested this manually and classes seems to be the same after applying the patch!
Comment #13
lauriiiReroll
Comment #14
davidhernandezI checked manually and everything comes out the same. I don't see any other issues.
Comment #17
lauriiiputting back
Comment #18
alexpottKey seems to be a really nondescriptive name. Let's call it css class since the docs of
template_preprocess_links()
saysThe key for each link is used as its CSS class
Comment #19
lauriiiRerolled and fixed #18
Comment #20
davidhernandezTested manually, and seem to still be getting the same classes. Code looks good. #19 includes the key name change.
Comment #21
alexpottI think we should change the variable name to link key to say what it is rather than what it is used for. And that way doing the clean_class in twig and not using drupal_html_class in the preprocess function makes sense.
Comment #22
davidhernandezSo change it from css_class to link_key?
Comment #23
Jens-0 CreditAttribution: Jens-0 commentedChanged variable name as suggested from css_class to link_key.
Comment #24
lauriiiSending to testbot :)
Comment #26
Jens-0 CreditAttribution: Jens-0 commentedComment #27
Jens-0 CreditAttribution: Jens-0 commentedComment #28
lauriiiFixes #21
Comment #29
alexpottI think I've missed a trick here. There is no need for this extra variable. We just shouldn't remove the keys from $variables['links']. So
becomes
Then we can use the key in the twig template.
Comment #30
lauriiiMaybe we finally could get this in ;)
Comment #31
davidhernandez#30 has the change in #29
Comment #32
catchCommitted/pushed to 8.0.x, thanks!