Updated: Comment #9
Problem/Motivation
Currently comment list is hardcoded into custom template comment-wrapper
with according preprocess function.
It's unclear which template should developer use - field or wrapper, both templates lacks some useful properties.
Proposed resolution
Use a field__comment
template suggestion to replace custom theme template.
Use field instance settings and template to render wrapper for comment list and form.
Remaining tasks
review theme suggestion and twig usage
optimize the render
User interface changes
no, just a markup
Before
After
API changes
no
Original report by @andypost
There's template conversion happens in #1898054: comment.module - Convert PHPTemplate templates to Twig
Todo:
1) Make changes in preprocess
2) follow template changes:
2.1) preprocess variables in @file doc-block
2.2) Replace <h2 class="title"><?php print t('Comments'); ?></h2>
with field name
Comment | File | Size | Author |
---|---|---|---|
#41 | 1962846-comment-wrapper-41.patch | 12.3 KB | andypost |
Comments
Comment #1
InternetDevels CreditAttribution: InternetDevels commentedJust a first try.
Comment #2
andypostComment #3
andypostComment #4
andypostIt looks like we need 2 settings:
1) label for comment list
2) label to add new item to that list
Comment #5
andypostRelated #1987396: Refactor & Clean-up comment.module theme functions
could remove comment-wrapper template so following
Comment #6
rteijeiro CreditAttribution: rteijeiro commentedLet's try this one :)
Comment #7
catchRemoving the comment wrapper template sounds good.
Comment #8
andypostInitial stub
Converts template for contextual field template
Comment #9
andypostproper title and summary
ps: related #2031883: Markup for: comment.html.twig
Comment #10
markhalliwellFirst, this is awesome!! Definitely +1 (this is how the theme system should be used in the first place). A few notes from briefly scanning the patch in #8:
This shouldn't be necessary. Theme registry detects template suggestions and adds the necessary theme suggestion hook referring to the base hook (in this case: field). See: drupal_find_theme_templates()
This, unfortunately, does not yet currently work (see parent issue). This is a perfect use case example of why we need preprocessing of theme hook suggestions put back in core.
We should pass an array here instead:
That being said, doesn't
$entity->getEntityTypeId() === 'comment'
? If so, shouldn't it really just be:Comment #12
andypost@Mark Carver Thanks a lot for review, I stuked with preprocess, because without theme hook definitions does not work and suggestion template does not work.
$entity
in formatter is a any entity where comment field is attached.3) Also it works with commented out
#theme
butsystem_theme_suggestions_field()
is wrong nowSo if someone will create field named comment (or will use "comment" as base entity field) then theme function will be confused.
Because both "type" and "name" could be same. So filed #2226233-22: Redo changes from field.module to Twig conversion issue that were clobbered
Comment #13
andypostFixed #10 1) and 2)
Comment #15
Wim LeersGood catch with deleting that one. Instead, from now on the one on the second line I cited should always be used.
Comment #16
markhalliwellI get that it works if we manually put this in the hook_theme definition.
That isn't the point nor is this "Best Practices™".
We shouldn't be putting suggestions inside hook_theme definitions. Let alone a "base hook". These are supposed to be detected automatically.
This is a perfect example of how broken the theme system is. The parent issue will fix the detection of preprocess suggestion functions.
Again, doesn't
$entity->getEntityTypeId() === 'comment'
? This seems redundant:field__comment__comment
edit: Furthermore, these actually aren't needed. I wasn't aware that
system_theme_suggestions_field()
existed. A simple'#theme' => 'field'
will suffice here.First, separate issue, but worth mentioning that this should be the field module that's providing these, not "system".
Second, this shouldn't be taken out. Fields are reusable across entity types. Consider:
Would you rather:
field--auto-upload-field.html.twig
field--node--auto-upload-field.html.twig
,field--comment--auto-upload-field.html.twig
,field--user--auto-upload-field.html.twig
,field--message--auto-upload-field.html.twig
edit: I understand there's a naming conflict, but this is specific to this issue. The solution is simple: name the field something that isn't the entity (like: "comments").
Comment #17
markhalliwellComment #18
andyposttaggin
Comment #20
markhalliwellComment #21
markhalliwellComment #22
andypost@Mark seems this one should be postponed on #939462: Specific preprocess functions for theme hook suggestions are not invoked
Without special preprocess functions we can't move forward
Comment #23
markhalliwellCorrect
Comment #24
andypostthis does not work now, because there's a lot of base fields now available so at least this dimension needs separate prefix
$suggestions[] = 'field__field_' . $element['#field_name'];
at the same time we could use some kind of "context" (plugin bag) to populate preprocess function name suggestions, nearly the same we have on core token registry
Comment #25
larowlanRelated #2228763: Create a comment-type config entity and use that as comment bundles, require selection in field settings form is in
Comment #26
andypostThere's more node fields have own (separate) templates now.
I think better to fix the issue with @todo linked to parent issue
Comment #27
andypostI see no actual reason to postpone the issue anymore, so another approach
Comment #29
andypost5 failures shows that now we see "Comment settings" as field named!
Here is a question - is there any ability to provide different label for widget and formatter?
Comment #30
andypostSuppose better to tune the widget title
Comment #32
andypostShould fix remaining tests.
RDF attributes are added to each field item, so I found only this way to merge first item attributes to main attributes
So the markup change is
Before
After
Comment #33
andypostComment #34
Wim LeersAwesome! Thanks :) I could only find nitpicks:
Broken sentence :)
s/rdf/RDFa/
Why?
Is this a change in behavior, or is this a change to reflect the actual behavior?
This should also list
comment_display_mode
andcomment_type
?Comment #35
andypostFixed #34 (1,2,4)
@Wim #34.3 is needed to set proper label placement for comment thread title - comment field label is used for.
Comment #36
Wim Leers#35, thanks, manually confirmed your answer for #34.3. With all that out of the way, I can RTBC this.
(I'm only fixing a tiny typo introduced in #35 in this reroll, so we don't have to wait for another reroll/retest cycle.)
Comment #39
andypostComment #41
andypostre-roll
Comment #42
alexpottCommitted 49f4fdd and pushed to 8.x. Thanks!
Comment #44
markhalliwellThis was originally postponed on #939462: Specific preprocess functions for theme hook suggestions are not invoked because then we wouldn't have to deal with explicitly defining something that should be detected automatically when the theme registry is built.
That being said (and since this made it in), a follow-up should be created to remove this once that issue is resolved.
Comment #45
andypost@Mark I think better to add this to summary of the issue #939462: Specific preprocess functions for theme hook suggestions are not invoked because there's a lot of the same usage all over core (node base fields at least), so I think no reason to add a @todo for each case
Comment #46
markhalliwellNo. Issues that like do not need added scope creep. I've created the follow-up so it can be tracked and fixed later down the road, no @todo necessary.