Problem/Motivation
language-negotiation-configure-form.html.twig hardcodes a number of classes on the <div> that wraps each language_type. This is not very flexible if there is a need to manipulate the attributes for contrib or custom implementations.
Proposed resolution
Instantiate Attribute objects in template_preprocess_language_negotiation_configure_form() so that other modules/themes can add in attributes, and change the hardcoded classes in the template to use addClass().
The code can be manually tested by enabling the Language module and navigating to /admin/config/regional/language/detection.
Remaining tasks
Update patch to add the Attributes as part of the array so that they can be manipulated individually and printed from the Twig template with:
{{ language_type.attributes }}
User interface changes
n/a
API changes
n/a
Original report by @davidhernandez
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_language_negotiation_configure_form
template_preprocess_language_content_settings_table (There seems to be no template file to go with this?)
Twig Templates Modified
language-negotiation-configure-form.html.twig
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | interdiff.txt | 472 bytes | star-szr |
| #22 | 2329847-22.patch | 2.02 KB | star-szr |
| #20 | drupal-move-language-classes-from-preprocess-to-templates-2329847-20.patch | 2.06 KB | tuutti |
| #20 | interdiff-2329847-18-20.txt | 612 bytes | tuutti |
| #18 | drupal-move-language-classes-from-preprocess-to-templates-2329847-18.patch | 2.06 KB | tuutti |
Comments
Comment #1
davidhernandezComment #2
rachel_norfolkComment #3
davidhernandezIt looks like settings table is rendered in code. Is this something that needs to be converted into a Twig template?
Comment #4
rachel_norfolkProbably does, yes. Going to upload patch before starting that side of things, I think
Comment #5
star-szrIt looks like that is a newer theme function (added in #1810386: Create workflow to setup multilingual for entity types, bundles and fields) so it didn't end up under the umbrella of #1757550: [Meta] Convert core theme functions to Twig templates yet. Maybe we could tackle the Twigification + bananafication of that in a new issue under #1757550: [Meta] Convert core theme functions to Twig templates to keep this one streamlined?
Comment #6
rachel_norfolkOkay - this is a really basic change and more needs to be done to satisfy #5, most likely in a different issue...
Comment #7
rachel_norfolkComment #8
star-szrThanks @rachel_norfolk! Yes, I agree a separate issue makes the most sense.
This looks like a great start, a few points though:
In brief I think if we call it wrapper_classes in the template then this should probably be wrapper_attributes.
Longer version: It's not usually necessarily to initialize the attribute object like this. Currently attributes, title_attributes, and content_attributes automatically get initialized as Attribute objects. But in this case it's only so it can be used in the Twig template. A bit of an odd case.
The extra blank line here is probably not needed.
The alignment and indenting on the set tag is not consistent with what is shown in #2322163: [meta] Consensus Banana Phase 1, move CSS classes from preprocess to twig templates..
Don't add a space between
<divand printing{{ attributes }}, otherwise you will end up with extra whitespace in your tags and if there are no attributes you'll end up with<div >. The Attribute object automatically prints its own whitespace.Comment #9
rachel_norfolkThanks for those @Cottser - I'm only on my 3rd d8 patch and LOTS to learn!! I think an entertaining final night of #fudk helps with the enthusiasm but maybe not so much with my code accuracy!!
I have incorporated the things you have mentioned in #8.
Comment #10
star-szrGreat to have you here learning with us! The changes look great, I think there's only one more thing that I would change, otherwise looks great to me.
The "before" code doesn't put language_type.type through any kind of clean_class type thing, so I think we can remove the clean_class filter from language_type.type for the time being.
Comment #11
tuutti commentedComment #12
star-szrThe interdiff looks backwards but the patch looks correct, thanks @tuutti! Although looking outside the context of the lines changed, I suppose we should also document in the template docblock that the wrapper_attributes variable was added.
You can copy + paste the wrapper_attributes docs from textarea.html.twig.
Comment #13
tuutti commentedComment #14
star-szrI manually tested this one on /admin/config/regional/language/detection, looks good and code and docs look good.
Thanks @rachel_norfolk and @tuutti!
Comment #15
star-szrTalked to @alexpott about this one in IRC, the classes are already in the template – the only thing is they are not using Attribute. I think this type of case will need more discussion as to how we handle them in phase 1 and phase 2.
If we stick with this type of approach of adding Attribute where it wasn't there before, then the wrapper_attributes should probably be language_type.attributes so that it's more flexible/beneficial for later preprocess functions to work with. As it is later preprocess functions can manipulate wrapper_attributes but that would apply to all language_types.
Comment #16
star-szrShifting this to be not a "banana" issue after discussion on tonight's Twig call. This is just a regular ol' cleanup issue now, and I think we just need to implement what I mentioned in the second paragraph of #15 and we could be done.
Comment #17
star-szrComment #18
tuutti commentedComment #19
dawehnerLet's use () as well.
Comment #20
tuutti commentedComment #21
star-szrComment #22
star-szrI just added manual testing steps to issue summary. This checks out and the code looks good. Thanks again!
Minor: Attached patch gets rid of the unnecessary line deletion.
Comment #23
alexpottCommitted fb4e8cd and pushed to 8.0.x. Thanks!