hide a user profile depending on role or a custom field
Task · hide information based on user role · theme based on user role · Developers and coders · Themers · Drupal 4.5.x or older · Drupal 4.6.x · Drupal 4.7.x
Last modified: August 27, 2009 - 00:23
description
This collection of snippets allow you to hide/show users based on user roles or a custom profile field.
Useful if you want the user list pages to ignore the SITE ADMINISTRATOR (UID 1) profile or if you want to hide certain users from being listed.
Usage
- For use in your profile_profile.tpl.php page override
- Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the snippet into your profile_profile.tpl.php file
- Tested and works with Drupal 4.5 and 4.6
Step 1 of 2
Put this at the very top of your profile_profile.tpl.php file: (This example uses a custom checkbox profile field called profile_hidden to determine if the profile should be displayed or hidden)
<?php if((!$user->profile_hidden) == '1'): /* check to see if the profile_hidden field is selected */?>Alternatively
Alternatively use this snippet if you want to hide users with a specific role type (the example below hides users with the role type Site Admin from the user list)
<?php if (!in_array('Site Admin', $user->roles)): /*only display users who do not have the user role type of Site Admin */ ?> Step 2 of 2
Put this at the very bottom of your profile_profile.tpl.php file
<?php endif; ?>Another option
Instead of
<?php if (!in_array('Site Admin', $user->roles)):
/*only display users who do not have the user role type of Site Admin */ ?>use this:
<?php
/*
This code will check for an argument role=[a number] in the query string
for instance, [hostaddress]/en/profile?role=3
It queries the roles database to find that role and then shows profiles only if they are a member of that role.
If no role is entered in query string, it returns all profiles
*/
$role = $_GET['role'];
if ($role<>""){
$sql = "select name from role where rid=".$role;
$result = db_query($sql);
$roleResult = db_fetch_object($result);
$roleName = $roleResult->name;
}
?>
<?php if (empty($role) or in_array($roleName, $user->roles)):
/*only display users who have the user role submitted in the query string*/ ?>