Jump to content

Adding body classes to admin theme according to user role


Robin S
 Share

Recommended Posts

This is something I used in a couple of modules recently and it could be useful as a standalone hook. It adds classes to the body tag in the admin theme for:

  • roles the user has
  • if the user is a non-superuser
  • the user's name

You could use this if you want to tweak the admin interface for a role using CSS/JS (e.g. with Admin Custom Files).

There is also an old wishlist item requesting this.

$this->addHookAfter('AdminTheme::getExtraMarkup', function($event) {
    $theme = $event->object;
    $user = $this->user;
    foreach($user->roles as $role) {
        $theme->addBodyClass("role-{$role->name}");
    }
    if(!$user->isSuperuser()) $theme->addBodyClass("role-nonsuperuser");
    $theme->addBodyClass("user-{$user->name}");
});

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...