Problem/Motivation
Currently, the only template suggestion available for theming users is user.html.twig. Because users can have view modes it makes sense to allow view-mode specific templates, in the same way you can with nodes.
Proposed resolution
Implement hook_theme_suggestions_HOOK() to add view-mode specific suggestions for user templates, so that the template suggestions are:
- user--viewmode.html.twig
- user.html.twig
Remaining tasks
Review the patch.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 2766379-17.patch | 1.72 KB | joelpittet |
| #17 | interdiff.txt | 1.81 KB | joelpittet |
| #8 | interdiff.txt | 1020 bytes | smaz |
| #8 | user-view-mode-templates-2766379-8.patch | 1.73 KB | smaz |
Comments
Comment #2
smazPatch attached.
Comment #3
smazAdding issue tags.
Comment #4
smazUpdated patch: I've now added an automated test.
Based on the fact that the node module has a test for templates (NodeTemplateSuggestionsTest), I've created a test for user template suggestions based on that.
Comment #5
smazSorry, the last patch didn't have the test included - added now.
Comment #7
erik.erskine commentedThe approach looks sound and adding a custom
user--full.html.twigtemplate worked for me.A couple of things in the test:
Did you mean
node, oruser? Either way, I'm not sureEntityViewBuilder::view()expects an entity name prepended like this, it looks a bit strange but it does work in this example.\Drupal::entityTypeManager()could be used as a straight swap for\Drupal::entityManager()here, to avoid the deprecated function call.Also worth looking at https://www.drupal.org/node/2354645 to see the template naming conventions for other entities. They do all seem to have slightly different suggestions, but is there anything else to consider, e.g. user ID/bundle name?
The documentation on that page would need updating too.
Comment #8
smazThanks for the review!
1) I did mean user - fixed. I'm not sure on the entity name being prepended, but it was that way in the NodeTemplateSuggestionsTest so I assumed it was for a reason.
2) Updated to use \Drupal::entityTypeManager().
I've looked at the template naming conventions - I'm not sure if user-ID specific templates would be required, if people haven't needed (or asked for) view mode specific ones yet? I also wasn't sure if the additional suggestions would have a performance impact in scanning for the suggestions (and if that's why it hasn't been done yet), so figured it was best to start simple. I can add the additional suggestions if people want, or it could be added as a followup.
Cheers
Comment #9
erik.erskine commentedPatch in #8 looks good.
This is a nice enhancement and brings theming of user entities more closely in line with other entities such as node/taxonomy etc.
This probably needs some input from the theme system maintainers, so tagging accordingly.
Comment #10
erik.erskine commentedComment #12
joelpittetMimics node suggestions in
node_theme_suggestions_node()and has a test as well.Comment #14
smazLooks like a random testbot issue?
Comment #16
claudiu.cristeaLet's use array square bracket syntax.
Comment #17
joelpittetThanks @claudiu.cristea, I've just changed the arrays to short syntax and added the test method public visibility.
Comment #18
smazManually tested & continues to work as expected. Square brackets now in use as per #16, so think this is good to go back to RTBC.
Cheers!
Comment #19
catchMoving back to CNR for the same reason as #2767243: Create a theme suggestion for taxonomy terms by view mode.
Comment #20
lauriiiSorry folks, but I think this should be fixed here instead: #2270883: Automatically add theme hook suggestions for all entity types