Drupal 8 Tip: Adding TWIG template suggestions for user profiles

Megan's picture

She has: 11,419 posts

Joined: Jun 1999

By default, there are no view mode template suggestions for user.html.twig. To add them, use hook_theme_suggestions_hook_alter:

function ts_conference_theme_suggestions_user_alter(array &$suggestions, array $variables) {
  $view_mode = $variables['elements']['#view_mode'];
  $suggestions[] = 'user__' . $view_mode;
}

There is a feature request for this, and it looks like it will be ready soon.

There is also no view_mode variable implemented by default. To do this, add it in your template_preprocess_user function:

function yourtheme_preprocess_user(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
}