Show block only to a particular role or roles

Show block to only one role

<?php
global $user;
if (
in_array('Approved Role',$user->roles)) {
  return
TRUE;
} else {
  return
FALSE;
}
?>

show block to more than one role

<?php
global $user;
$allowed = array('moderator','administrator');
$valid=FALSE;
foreach(
$user->roles as $role){
  if(
in_array($role, $allowed)) {
   
$valid=TRUE;
  }
}
return
$valid;
?>

Show to authenticated user in no role and not to authenticated user in roles

(This snippet is quite helpful in Drupal 5. If you want to show the block to authenticated users who have no other role but not to other authenticated users who do have a role, the checkboxes will be of no help.)

<?php
global $user;
// These are the roles who should not see this block
$notallowed = array('anonymous user','artist','GoZabo Administrator', 'SuperAdmin');

// Assume they can see it to start
$valid=TRUE;

// Go through each role the user is in and, if we hit a role that is not allowed to see it, set valid to false
foreach($user->roles as $role){
  if(
in_array($role, $notallowed)) {
   
$valid=FALSE;
  }
}

// If no roles were hit that aren't allowed, this will still be true. Otherwise it will be false.
return $valid;
?>

Show block only to a particular user:

<?php
global $user;
if (
$user->uid == 1){
  return
TRUE;
} else {
  return
FALSE;
}
?>

Variations: Replace the “1” with any UID.

Show block only for specific content type:

<?php
$match
= FALSE;
$types = array('story' => 1, 'page' => 1);
if (
arg(0) == 'node' && is_numeric(arg(1))) {
 
$nid = arg(1);
 
$node = node_load(array('nid' => $nid));
 
$type = $node->type;
  if (isset(
$types[$type])) {
   
$match = TRUE;
  }
}
return
$match;
?>

Variations: This example would show the block on all 'story' and 'page' type nodes. Just change line 2 - the $types array - to indicate which node types you want your block to appear on. Use the format 'nodetype' => 1 for each type you need. And yes, the array can hold single type only.

Combining PHP visibility control with specific page visibility:

When you choose the option to control visibility by PHP, you lose the ability to have Drupal automatically show/not show a block based on path. The following workaround is a bit complex and clunky, but it works.

1. On the page administer >> blocks, hover over the configure link for the block you want to control in order to view the URL. Most of the URLs will end in “configure/sometext/somenumberortext”. Make a note of the “sometext” and the “somenumberortext”.
2. Set the region for this block to "None". From now on let's call this block "Original Block."
3. Go to administer >> blocks >> add block (tab) . In the block description field (what shows up on the admin/build/block page) write something like "Original Block Name Visible".
4. Set the block body input type to PHP.
5. What we do here, in the body (not in the visibility settings) is that we set the non page orientated visibility settings with php code. If the visability settings we set are TRUE -- then the code calls to output the actual content of our now disabled, original block. Pretty tricky huh! As an example, we are going to use a simple visability snippet. Start out with this shell in the block body text area:

<?php
 
global $user;
 
$output = '';
  return
$output;
?>

6. Paste in just the “if” part from one of the snippets above. We’ll use the first snippet as an example. You should have something like this:

<?php
 
global $user;
 
$output = '';

  if (
$user->uid){
   
HERE
 
}

  return
$output;
?>

7. Add in the block calling code so it looks like this:

<?php
     
global $user;
     
$output = '';

      if (
$user->uid){
     
$block = SOMETEXT_block('view', SOMENUMBER);
     
$output $block['content'];
      }

      return
$output;
     
?>

8. Replace with the “SOMETEXT” and “SOMENUMBER” that you noted in step 1.
9. The final result should look something like this:

<?php
     
global $user;
     
$output = '';

      if (
$user->uid){
     
$block = menu_block('view', 10);
     
$output $block['content'];
      }

      return
$output;
     
?>

10. That takes care of showing the block only to logged in users (or whatever snippet you used). Next go to the “Page specific visibility settings” (you are still in this new block, not the original block) section and set one of the first two radio buttons as normal. Add the page specific setting in the usual place
11. Enable and place this new block and check to see that the original one has its region set to "none". Make sure you don't delete the original one. That's where the content is coming from. Examine the block descriptions for both of them and describe them in a way that, hopefully, won't confuse you later. It's a good idea to add commenting to the php snippet explaining exactly what you've done.

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.