What is the current best way to show/hide blocks based on role in 4.6? For example, I don't want the navigation block to show up for anonymous or even authenticated users, only for those with roles I have defined.

Is there a setting, module, or patch that would provide this? Should I create a new block and use php to put the logic within the block?

Comments

michelle’s picture

azote’s picture

i use:

 global $user ;

$roles = $user->roles;
$name=$user->name;
$approved_roles = array('admins','SuperPower');
if (count(array_intersect($roles, $approved_roles)) > 0) {
  return '

<div id="labmenu">
<ul>
<li class="leaf"><span class="lw1">hey '.$name.'  !? </span> </li>
</ul>
</div>';
}
LoveBeingADad’s picture

Thanks for the direction. I was hoping to see if there were new configuration controls to show/hide blocks rather than creating a block with the php to control visibility. Apparently not, so I'm off happily playing with the php. Just didn't want to do it the harder way if there were an easier way. Thanks for the responses.

edudx’s picture

Having just set up my first drupal site I worked myself through the forums looking for an "easier" solution. In fact, the PHP method described above works quite well. The only pain is when you add a new role and you have to go in and modify all your code.

Though inserting PHP is effective, I did find myself asking, wouldn't it be nice just to have a set of checkboxes on the block creation screen? These could easily be built into the definition of the block using the very same php code as is inserted now by hand. You'd run into the same problem in adding new roles, but it would be "easier" to update checkboxes than going into the code.

gbanse’s picture

I can see how the code provided by several folks would work well for User Added Blocks but how would one hide a Drupal Native Block (like Navigation)? I can't see a way to add code to it like I would to a block I create.

michelle’s picture

Create a new block with code like this:

global $user;
$output = '';

if (is_array($user->roles) && in_array('Approved Role',$user->roles)) {
  $block = user_block('view', 1);
  $output .=  $block['content'];
}

return $output;

Then disable the menu. For other blocks, look at the "configure" link for that block. If it says /configure/menu/42 then you want "menu_block('view',42);", etc.

Michelle

gbanse’s picture

Thank you!

gbanse’s picture

I created a new block using the code supplied.

Added two links to the output portion.

Changed the text 'Approved Role' to the role I'm trying to give permissions to view the block with.

Changed the " user_block('view', 1)" to match the block number.

Saved it.

Enabled it to make sure it worked and then disabled it.

But noone can view the block. What'd I mess up?

LateNightDesigner’s picture

If you create a new menu, you can declare it as a block. You have all the access controls for that menu that you do normally. Just declare what a role can do in the menu system and you have your own navigation block.

If the block has no active links it'll disappear.

I think it's far easier than fiddling with the php.

dww’s picture

i wrote a patch to the 4.6 version of block.module that allows site admins to just select via checkboxes what roles should be able to view any given block. see http://drupal.org/node/49154 for details. also, this functionality will be added to 4.7 sometime soon (see http://drupal.org/node/18018 for info on that discussion).

___________________
3281d Consulting