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:

  1. user--viewmode.html.twig
  2. user.html.twig

Remaining tasks

Review the patch.

Members fund testing for the Drupal project. Drupal Association Learn more


smaz created an issue. See original summary.

smaz’s picture

Status: Active » Needs review
626 bytes

Patch attached.

smaz’s picture

Issue tags: +Novice, +Quick fix

Adding issue tags.

smaz’s picture

Updated 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.

smaz’s picture

Sorry, the last patch didn't have the test included - added now.

The last submitted patch, 4: user-view-mode-templates-2766379-4.patch, failed testing.

erik.erskine’s picture

The approach looks sound and adding a custom user--full.html.twig template worked for me.

A couple of things in the test:

  1. +++ b/core/modules/user/src/Tests/UserTemplateSuggestionsTest.php
    @@ -0,0 +1,30 @@
    +    $view_mode = 'node.my_custom_view_mode';

    Did you mean node, or user? Either way, I'm not sure EntityViewBuilder::view() expects an entity name prepended like this, it looks a bit strange but it does work in this example.

  2. +++ b/core/modules/user/src/Tests/UserTemplateSuggestionsTest.php
    @@ -0,0 +1,30 @@
    +    $build = \Drupal::entityManager()->getViewBuilder('user')->view($user, $view_mode);

    \Drupal::entityTypeManager() could be used as a straight swap for \Drupal::entityManager() here, to avoid the deprecated function call.

Also worth looking at 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.

smaz’s picture

Thanks 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.


erik.erskine’s picture

Component: user.module » theme system
Issue summary: View changes
Issue tags: +Needs subsystem maintainer review, +Twig

Patch 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.

erik.erskine’s picture

Issue tags: +dcbristol2016

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs subsystem maintainer review

Mimics node suggestions in node_theme_suggestions_node() and has a test as well.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: user-view-mode-templates-2766379-8.patch, failed testing.

smaz’s picture

Status: Needs work » Reviewed & tested by the community

Looks like a random testbot issue?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: user-view-mode-templates-2766379-8.patch, failed testing.

claudiu.cristea’s picture

+++ b/core/modules/user/user.module
@@ -1426,3 +1426,13 @@ function template_preprocess_user(&$variables) {
+  $suggestions = array();

Let's use array square bracket syntax.

joelpittet’s picture

Status: Needs work » Needs review
1.81 KB
1.72 KB

Thanks @claudiu.cristea, I've just changed the arrays to short syntax and added the test method public visibility.

smaz’s picture

Status: Needs review » Reviewed & tested by the community

Manually tested & continues to work as expected. Square brackets now in use as per #16, so think this is good to go back to RTBC.


catch’s picture

Status: Reviewed & tested by the community » Needs review
lauriii’s picture

Status: Needs review » Closed (duplicate)

Sorry folks, but I think this should be fixed here instead: #2270883: Automatically add theme hook suggestions for all entity types