Drupal 7: Add a class to the body

Drupal Today I had to add a class to the body so know if I was logged in as “administrator” like role, it needed to have the permission to modify any content of the type MYTYPE.

To add a class to the body you have to work on the template_preprocess_html for your theme, like the snippet below.

function MYTHEME_preprocess_html(&$vars) {
  if(user_access("edit any MYTYPE content")){
    $vars['classes_array'][] = "MYCLASS";
  }
}

The user_access() will check if the current user (if there isn’t one specified) has the selected permission, the permission is the one you can see from the permission administration page, if you are using Drupal in another language then refer to the base English interface to know what to write there.

Enjoy!

Advertisements

Your opinion is important!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s