6

I used hook_theme_suggestions_HOOK_alter:

function MYTHEME_theme_suggestions_user_alter(&$suggestions, $vars, $hook) {

  // Define the view mode.
  $mode = $vars['elements']['#view_mode'];

  // Create a theme hook suggestion which has the view mode name in it.
  $suggestions[] = 'user__' . $mode;
}

Now I can work with user--full.html.twig and user--compact.html.twig

But I have a role editor and I want to user--compact--editor.html.twig to be picked up. How can I add a template suggestion based on user roles?

0

1 Answer 1

12

All you need to do is to get the current user object, get its roles, and then add the suggestion like in the following snippet. The current user also already is available in the $variables.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_user_alter(array &$suggestions, array $variables) {

  // Define the view mode.
  $view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  // Create a theme hook suggestion which has the view mode name in it.
  $suggestions[] = 'user__' . $view_mode;
    
  // Get current user.
  $current_user = $variables['elements']['#user'];

  // Get current user's roles.
  $roles = $current_user->getRoles();

  // Add suggestion per role.
  foreach ($roles as $role) {
    $suggestions[] = 'user__' . $view_mode . '__' . $role;
  }
}

This will give you:

<!-- FILE NAME SUGGESTIONS:
   * user--full--administrator.html.twig
   * user--full--authenticated.html.twig
   * user--full.html.twig
   x user.html.twig
-->

BUT! The problem with this approach is what do you want to happen when a user has multiple roles?

5
  • Nice. @leymannx would you be willing to be a maintainer of drupal.org/project/twigsuggest and add this?
    – mlncn
    Commented Nov 20, 2018 at 17:00
  • @mlncn – Yes for the maintainership, as this really seems to be a useful module. About the snippets here, after I dropped that answer I thought to myself: What if a user has multiple roles, and there are multiple templates, how to decide which should be taken into account? Will it take just the first in alphabetical order? Worth some testing.
    – leymannx
    Commented Nov 20, 2018 at 17:03
  • @lemannx excellent, done, thank you. Oh, yeah, of course, on the multiple roles. I was also thinking if there might be performance issues with getting roles... and now we're in the territory of building a UI for deciding if/which roles to do suggestions for... maybe only if more need is demonstrated!
    – mlncn
    Commented Nov 20, 2018 at 17:12
  • @mlncn – If roles in the UI are used as intended (with kind of an order from left "anonymous" to right "admin") we maybe only add one suggestion per user for its "highest" role?! Let's discuss this in the module's issue queue.
    – leymannx
    Commented Nov 20, 2018 at 18:09
  • A little cleaner is to use the following (also check if they are available) ``` if ($current_user instanceof User) { ``` && ``` if (isset($variables['elements']['#view_mode'])) { ``` Commented Mar 3, 2021 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.