hide a user profile depending on role or a custom field

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; ?>

This snippet works for a

keizo - May 6, 2006 - 04:10

This snippet works for a quick hack, but it seems to break the pager since it happens in the template. For example if I only want to show them if people fill in their real name. So like:

<?php if(($user->profile_name) != ''): /* check to see if name is set */?>

The problem is that a majority have it has nothing so only a few profiles show on each page. I haven't thought much on a solution, but will post here if I figure something out.

Filtering my profiles

Eben J. Muse - June 22, 2006 - 13:36

I want to do the exact opposite of this. I would like to show profiles for everyone that has a specific role. Each member has a department which controls what content they can maintain. I've used roles to control that access, so now I want to list the profile for everyone in each role.

I can use the alternative snippet above and just change
(!in_array('

to
(in_array('

That is, remove the ! operator.

But that only works for one role at a time. How can I automate the process? Ideally, I would like a argument attached to the profile and use that in a case structure, but I don't know how to access the arguments in Drupal, except in views.

Any suggestions?

Eben

Solved it `

Eben J. Muse - June 22, 2006 - 14:42

I've replace the original line

<?php if (!in_array('Site Admin', $user->roles)): /*only display users who do not have the user role type of Site Admin */ ?>

with the following.
<?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*/ ?>

That allows me to do what I needed.

Eben

 
 

Drupal is a registered trademark of Dries Buytaert.