Use PHP Snippet to Show Block for Certain Roles, on Certain Pages and Only to Users in Certain Groups

This snippet came about because I needed to show a block to users in a very specific circumstance.

Users have a hidden field in their content profile page. This field is a node reference to a content type called "county".
Users can only be in one county.

1. Show only to users not in specified roles.
2. Show only on either a certain path OR on a certain content type page.
3. Show only on a county content type page IF the user is on his own county page.

<?php
global $user;
if(!
$user->uid) {
  return
FALSE;
}

$notallowed = array('local coordinator', 'county coordinator', 'regional coordinator', 'state coordinator', 'staff', 'staffer');
$valid_role = TRUE;
foreach(
$user->roles as $role){
  if(
in_array($role, $notallowed)) {
    return
FALSE;
  }
}

$county_page = FALSE;
$types = array('county' => 1);
if (
arg(0) == 'node' && is_numeric(arg(1))) {
 
$nid = arg(1);
 
$node = node_load(array('nid' => $nid));
 
$current_county_nid = $node->nid;
 
$type = $node->type;
  if (isset(
$types[$type])) {
   
$county_page = TRUE;
  }
 
// get users county nid
 
$county_node = node_load(array('type' => 'settings', 'status' => 1, 'uid' => $user->uid));
  if (isset(
$county_node->field_prof_county[0]['nid'])) {
   
$users_county_nid = $county_node->field_prof_county[0]['nid'];
  }
 
// is user on his own county page?
 
if($current_county_nid == $users_county_nid) {
   
$on_users_county = TRUE;
  }
}

$curr_uri = request_uri();
if (
$curr_uri == '/updates') {
 
$updates_page = TRUE;
}

if(
$updates_page || ($county_page && $on_users_county)) {
  return
TRUE;
} else {
  return
FALSE;
}
?>

Wow... that's a lot of logic just to show (or not) a block.

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.