Hey all. I want to theme a node slightly different depending on the role of the node's author. Specifically, if the author is of a particular role, I want to display some additional content. How do I accomplish this? I can get the author's uid... and I've tested for the role of the currently logged in user, but I'm stumped. Any code snippets that someone can share?

Thanks.

Comments

rszrama’s picture

The problem is a user's roles are stored in an array, so it's not a simple a = b situation.

Post the following snippet in a PHP page in your site to see what I mean:

  global $user;
  print '<pre>'. print_r($user, TRUE) .'</pre>';

Toward the bottom will be the roles array. So, you can poll through the roles a user has and if a certain role is detected then theme the page differently:

  $user = user_load(array('uid' => $node->uid));
  foreach ($user->roles as $rid => $role) {
    if ($role == 'authenticated user') {
      drupal_set_message('This is an authenticated user.');
    }
  }

Obviously, you'll need to modify this... but the premise works.

----------------------
Current Drupal project: http://www.ubercart.org

prattboy’s picture

That works great with one exception: when using it, it would log the user in as the author of the page if they viewed a page with that code in it. I suspected that it was due to me calling global $user; earlier in the document.

So I just changed $user in the snippet you gave to something else and it worked a-okay. Any concerns I should have with doing that?

Thanks for the quick response; I appreciate it.

rszrama’s picture

No worries, you were right to change $user from my snip to something else. : )

----------------------
Current Drupal project: http://www.ubercart.org

sylvie_n’s picture

Hi there,
I can see your code is what i need...but not sure how to modify it and where to put it.
if say i wanted to print the role next to the author on a tracker page what would i do exactly?
thanks

rszrama’s picture

Unfortunately, you can't really add it to existing pages easily like that. One thing you might try is using the Views module to override the tracker page. Doing that lets you add extra fields and things, and it might just work for user role.

----------------------
Drupal by Wombats | Current Drupal project: http://www.ubercart.org

nevets’s picture

Your existing node.tpl.php will have a section something like

  <?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted ?></span>
  <?php endif; ?>

We are going to change this to

  <?php if ($submitted): ?>
    <span class="submitted"><?php print t('Submitted by !a @r on @b.', array('!a' => theme('username', $node), '@r' => $role, '@b' => format_date($node->created)))?></span>
  <?php endif; ?>

This uses the original code from the phptemplate engine add adds the role.

For this to work you will need to add the snippet to the beginning of node.tpl.php and use that to set $role to the information you want printed after (in the example) the users name

droople’s picture

link