Jump to content
Robin S

Adding body classes to admin theme according to user role

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 4

Share this post


Link to post
Share on other sites

Would you mind if I add this to AdminOnSteroids? (probably won't be configurable)

  • Like 3

Share this post


Link to post
Share on other sites
4 minutes ago, tpr said:

Would you mind if I add this to AdminOnSteroids?

Go for it.

  • Like 2

Share this post


Link to post
Share on other sites

Went for it, thanks! :)

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...