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_rdf_metadata
Twig Templates Modified
rdf-metadata.html.twig
Comment | File | Size | Author |
---|---|---|---|
#17 | move_rdf_classes_from-2329787-17.patch | 1.49 KB | cilefen |
#17 | interdiff-12-17.txt | 1005 bytes | cilefen |
#14 | move_rdf_classes_from-2329787-14.patch | 1.4 KB | cilefen |
#14 | interdiff-12-14.txt | 561 bytes | cilefen |
#12 | move_rdf_classes_from-2329787-12.patch | 1.34 KB | cilefen |
Comments
Comment #1
davidhernandezComment #2
scor CreditAttribution: scor commentedComment #3
tuutti CreditAttribution: tuutti commentedComment #7
star-szrThanks for the patch @tuutti!
This would result in double attributes. Should be:
<span{{ attributes.addClass(classes) }}></span>
Comment #8
tuutti CreditAttribution: tuutti commentedComment #10
cilefen CreditAttribution: cilefen commentedIt looks as though $attributes are not set sometimes when $variables is passed to the prerprocess function. The non-patched file sets $attributes['class'] to an array every time so it was not noticed. This patch sets $attributes to an empty array if it was not set.
Comment #11
star-szrThanks @cilefen!
I think we should probably add a short code comment to explain why this is being added.
Since this is just two simple classes, maybe we should just do it inline?
Comment #12
cilefen CreditAttribution: cilefen commentedComment #13
star-szrThanks!
To me that code comment just explains the code but not the "why", maybe something along the lines of:
In some cases $attributes is not set, in those cases make it an array for when we construct the Attribute object below.
Edit: or, shorter:
If $attributes is not set make it an empty array for constructing the Attribute object below.
Comment #14
cilefen CreditAttribution: cilefen commentedComment #15
star-szrMakes sense to me, thanks @cilefen!
Comment #16
alexpottIt is not that it is not set. It is that is null. This could be a if else too just set $variables['metadata'][$key] = new Attribute(); and then the comment could be shorter. Also the comment below about the doctype looks like it should be move to the twig template - can someone open a followup.
Comment #17
cilefen CreditAttribution: cilefen commentedThose are good suggestions @alexpott. I think we can do without a comment altogether this way.
#2336965: Move the XHTML inline comment in template_preprocess_rdf_metadata to the template file
Comment #18
star-szrThat seems reasonable to me, is_array() might be better than !is_null potentially but that's about it. Not sure if there any performance differences between the two.
Comment #19
alexpottCommitted b29ad66 and pushed to 8.0.x. Thanks!