Insert all roles of current user as classes in body field

Created on 13 May 2025, about 21 hours ago

Problem/Motivation

In the body tag, a user-logged-in class is added if the user is logged in, which is great.

This is the difference between standard (anonymous) and a logged in user:

<body class="path-node page-node-type-page">
<body class="path-node page-node-type-page user-logged-in">

This is useful in many situations, but sometimes you want to add more granular, role-specific CSS. For example, to hide some fields or elements for some user roles, while showing it to other roles.

You can add a function in the theme-file (see below), but not everyone has the ability to do that, and overall the classes are useful in many use cases.

Steps to reproduce

Want to add CSS targeting specific user roles, but can't.

Proposed resolution

Insert each role of the current user into the Body field, like this:

Anonymous user, extra class anonymous-role:
<body class="path-node page-node-type-page anonymous-role">

Logged in user, extra classes developer-role and editor-role:
<body class="path-node page-node-type-page authenticated-role developer-role editor-role user-logged-in">

Workaround

Insert this code in my_theme.theme file (replace my_theme):

function my_theme_preprocess_html(&$variables) {
  $current_user = \Drupal::currentUser();
  $roles = $current_user->getRoles();
  foreach ($roles as $role) {
    $variables['attributes']['class'][] = \Drupal\Component\Utility\Html::cleanCssIdentifier($role . '-role');
  }
}

Note that this needs to be updated eventually:

template_preprocess_html will be deprecated in drupal 11.2.0 and removed from drupal 12. template_preprocess functions are registered directly in hook_theme().

From function template_preprocess_html.

Originally posted in https://www.drupal.org/forum/support/post-installation/2015-12-07/add-bo... β†’

Remaining tasks

Add the function above in Drupal core themes, so that roles are added as classes in the Body field.

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡©πŸ‡°Denmark ressa Copenhagen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ressa
  • Merge request !12109Add current user roles as classes in body β†’ (Open) created by ressa
  • Pipeline finished with Failed
    about 21 hours ago
    Total: 105s
    #495872
  • Pipeline finished with Failed
    about 21 hours ago
    Total: 203s
    #495876
  • Pipeline finished with Success
    about 20 hours ago
    Total: 545s
    #495889
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen
  • Pipeline finished with Failed
    about 19 hours ago
    Total: 127s
    #495981
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen
  • Pipeline finished with Success
    about 19 hours ago
    Total: 376s
    #495987
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Updating Issue Summary after switching to a template preprocess variable and Twig-based solution.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    There any security concern with putting user information into the dom?

  • πŸ‡ΊπŸ‡ΈUnited States TolstoyDotCom L.A.

    As long as this is just for logged-in users, putting their roles in the DOM seems to be analogous to putting any other user-specific info in the DOM.

    However, a site might have a 'shadowbanned' role, and admins might be too cowardly to reveal that to the user. On the justifiable front, a site might have security levels and wouldn't want to reveal to a user which level they're on.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I think this should be closed as won't fix for the following reasons

    • It doesn't meet the needs of the 80% use case
    • It can easily be achieved with existing APIs for sites or themes that need it
    • It causes a lot of cache fragmentation because we're now varying the page cache in every site by each unique combination of roles the user can have. Making all sites pay this cost for the low fraction of sites that need it is the main reason to close this in my opinion (note th current patch doesn't add the user.roles context to the #cache array, but should)

    Thank you for taking the time to open this issue, for sharing your solution and for creating an MR for consideration

Production build 0.71.5 2024