I'm building a website on Drupal 7 which will be accessible only to registered users. A taxonomy term is referenced to the user account. I need to add a body class based on that taxonomy term so I can personalize the theme's look depending on the logged in user.

2 Answers 2


In your custom module implement hook_preprocess_field().In the given code I am adding a taxonomy term field with uid as class,you can change to your requirement.

function mymodulename_preprocess_field(&$variables) {
  global $user
  if($variables['element']['#field_name'] == 'your_taxonomy_term_name') {
    $variables['classes_array'][] = $user->uid;

Here is how I did it.

function mytheme_preprocess_html(&$variables) {
  global $user;
  $user = user_load($user->uid); // Load user
  $variables['classes_array'][] = $user->field_myfield['und'][0]['tid'];

It will output the tid not the term name that is being referenced in the user account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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