To show a block to a one or more roles, put this code inside the block under Page specific visibility settings
Select -> Show if the following PHP code returns TRUE (PHP-mode, experts only).

Code

<?php
global $user;
$approved_roles = array('board', 'sales', 'engineer');

if (is_array($user->roles)) {
  if (count(array_intersect($user->roles, $approved_roles)) > 0) {
    return TRUE;
  } else {
    return FALSE;
}}
?>

Users with the role 'board', 'sales' or 'engineer' will see block.

Sometimes you may want to hide a block for a certain role and show it to all others.

<?php
global $user;
$approved_roles = array('board', 'sales', 'engineer');

if (is_array($user->roles)) {
  if (count(array_intersect($user->roles, $approved_roles)) > 0) {
    return FALSE;
  } else {
    return TRUE;
}}
?>

Comments

yousername’s picture

Hello!

I am looking for a way to display a custom block on profiles of users that belong to a particular role. The block should be visible to anyone, but only displayed on profiles of users who have role x.

My searches returned no results.
Any hints in the right direction greatly appreciated.

PS
I am not a programmer, so plese ask for more specific info if needed.

Thank you!

Satri’s picture

Just a quick thank you, this is what I needed. This type of documentation is very useful :-) -- Alex