Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Showing blocks on user profiles of particular role
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!
Thanks
Just a quick thank you, this is what I needed. This type of documentation is very useful :-) -- Alex