I need to show certain blocks to pre-authenticated users and authenticated users while hiding it from certain other roles.

The problem here is that everyone who registers automatically has the authorized user role, so there is no way to show a block to authenticated users while hiding it from other roles. This is not editable through Drupal's user interface.

Any ideas?

Comments

sillygwailo’s picture

Try the following code in your "Page specific visibility settings" for the block, using the "Show if the following PHP code returns TRUE" setting:

  global $user;
  return ((count($user->roles) == 1) && ($user->roles[1] == 'anonymous user' || $user->roles[2] == 'authenticated user'));

This should show if the user belongs to the 'authenticated user' role or the 'anonymous user' role and only one of those roles. Note that in my testing, the first user was affected, as it has the 'authenticated user' role and only that role, but not users that had either 2 or more roles.

(Username formerly my full name, Richard Eriksson.)

robhamm’s picture

Thanks, man. I also found another workaround. Since the blocks in question are ads that I don't want paid subscribers to see, I just set up a copy of Garland for which I set the blocks not to show, and made the paid subscriber role the only role that is able to switch themes. So when someone subscribes, they gain the new role and receive an email telling them how to change themes.

Your way, however, seems much better, as it is not theme-dependent. I'll try it out.

Rob Hamm
http://www.bluecrashkit.com (my current webcomic and means of support)
http://www.syberfang.com (my upcoming webcomic)

elijah lynn’s picture

Nice Richard!

This works great, thanks so much!

-----------------------------------------------
The Future is Open!

portait’s picture

Thanks Richard, your code works fine.
But how would I have to change the code in order that the block won't show up for "anonymous users" & the custom "privileged users" role.
In other words, the block should only show up in role "authenticated users" and hide from all other roles.

quickshare’s picture

Home » Administer » Site building » Blocks » Configure block.

In the "Role specific visibility settings" select "authenticated user" and save.

portait’s picture

This does not work because it will show to "privileged users". ;)

lovedrupal6’s picture

Any further updates on this topics please?

I am struggling with this.

Tilo’s picture

Hi
This is something strange in Drupal because conditions and roles are somehow mixed. However with this PHP code in the blocks visibility settings you can expose the block to anonymous and/or authenticated users but not to any higher role.

global $user;
$rc = count ($user->roles);
//Show block if user is authenticated but does not own any higher role
if (array_key_exists(2, ($user->roles)) && $rc == 1) {
   return TRUE;
  }
  //Show block to anonymous users
if   ($rc < 1) { 
    return TRUE;
    }

If you would like to show the block only to authenticated users, remove the second condition. If you would like to show the block only to anonymous users remove the first condition.

Hopefully this helps
Regards
Tilo

liliplanet’s picture

Thank you for this code which was posted early 2011. I've tried this in Drupal 7, (that is show a block to only anonymous and authenticated users and no other role) but does not seem to work.

Has something perhaps changed in D7? Most appreciate any help :)

global $user;
$rc = count ($user->roles);
//Show block if user is authenticated but does not own any higher role
if (array_key_exists(2, ($user->roles)) && $rc == 1) {
   return TRUE;
  }
  //Show block to anonymous users
if   ($rc < 1) {
    return TRUE;
    }
capmex’s picture

The simplest code I have found for showing a block to only anonymous or authenticated users with no other role is:

<?php
global $user;
if(count($user->roles) == 1) {
  // User is anonymous or has only the authenticated user role
  return TRUE;
}
?>
badrun’s picture

Use "Hide Block by Role" module
http://drupal.org/project/hide_block_by_role