Score:0

How to add a user role as a body class to the admin theme, Seven?

cn flag

I need to add a user role class to the body tag in the Seven theme, our admin theme. What is the best way to manage that without touching Seven? Is it even possible?

Ultimately, I'm trying to hide a field element in the Manage Form Display from showing to certain roles so as to avoid confusion for that role, but I want to keep it for Administrators. So I'm open to other options for this.

4uk4 avatar
cn flag
Does this answer your question? [Add the user role to the body class](https://drupal.stackexchange.com/questions/195203/add-the-user-role-to-the-body-class). You can put the hook in a module as well, if you don't want to touch the theme.
user3038672 avatar
cn flag
Thank you. However, this works for the primary/default theme, but it is not adding the role for the admin theme, which is Seven (probably bc the preprocess function is being added to the default theme file, and not Seven). Ultimately, I'm trying to hide a field element in the Manage Form Display from showing to certain roles so as to avoid confusion for that role, but I want to keep it for Administrators.
user3038672 avatar
cn flag
4k4, I did try the module route and that worked! Thank you!
Score:1
cn flag

You can add the class to the body attributes in a hook_preprocess_html(). See Add the user role to the body class

Hooks of the front-end theme are not invoked in the back-end and vice versa. You would need to sub-theme Seven if you want to place a custom hook. Or put the preprocess hook in a module. In this case you can check the current theme, if you don't want to add the class to all themes. See Find out which theme is loaded in a given page

This is not the only way to solve the second part of the question and as always Drupal has a lot more. You could for example set #access to FALSE in a form alter hook.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.