0

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.

3
  • Does this answer your question? 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.
    – 4uk4
    Sep 2, 2021 at 18:28
  • 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. Sep 2, 2021 at 18:44
  • 4k4, I did try the module route and that worked! Thank you! Sep 2, 2021 at 18:51

1 Answer 1

1

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.

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