Back to top

3 lines of code with Panels3 and Profile modules to create user configurable privacy settings

Goal

Create a checkbox option to let individual user to hide/un-hide their profiles on a Drupal site (Drupal 6).Public User ProfilePublic User Profile

Analysis

1. We need a field (1 bit) to store each users' preference (true or false)
2. We need a controller to toggle/switch the display of the user's profile. Particularly, the logic should follow the pseudo-code below:

IF
                   (User.Profile IS Public OR Being_Viewed_User IS SAME AS Logged_In_User)
            THEN
                   display the User.Profile
            ELSE
                   Print a message saying the User.Profile is Private

Building

The above specification is very easy to implement in a customized Drupal module. However, with extensibility and re-usability in mind, why not use existing modules with minimal configuration?

1. Drupal 6 Profile module gives admin easy UI to create a checkbox to store personal values. Let's create one called Make my profile private, and provide some description like Check the box above to make your profile private. To integrate your BeerCloud with social media networks such as Facebook, Twitter, and Google Buzz, you must keep your profile public.. Then, make it private field, and Visible in user registration form.
Make my profile privateMake my profile private

2. Use Panels3 to overwrite User Profile page (/users/%). Then create two variant displays for two kinds of conditions above (If ... then ... else).
User Profile Panels OverwriteUser Profile Panels Overwrite

3. In the variant for displaying the user profile, make two Selection Rules, and use OR selector:
User: compare User being viewed is Logged in user, return true if they are same
PHP code:check privacy settings

if ( arg(0) == 'user') {
$privacy = db_result(db_query('SELECT value FROM {profile_values} WHERE fid=9 AND uid=%d', arg(1)));
return !$privacy;
};

Please notice the field id "fid=9", in my case the "Make my profile private" has fid 9. You probably will have a different number; so go to your profile_value table in your website database to find that value

4. In the other variant for displaying some message like "The profile for GreatBrewers.com user %user:name is private. If you would like to contact %user:name, please click "contact" tab above.":
Again, create a PHP code:check privacy settings Selection Rule:

if ( arg(0) == 'user') {
$privacy = db_result(db_query('SELECT value FROM {profile_values} WHERE fid=9 AND uid=%d', arg(1)));
return $privacy;
};

Please notice the code in Section 4 is almost identical to the code in Section 3, except the returning value is the negated

Save. Save. and Save. That's it. Only 3 lines of code, your Drupal site has the configurable user privacy settings

Comments

I'm having trouble finding

Comment: 

I'm having trouble finding the FID for my checkbox. I'm using the content profile module, so I added the checkbox using CCK (not the profile module).

Does anyone know what table to look in for the FID?

Thanks

If you are using Content profile

Comment: 

If you are using Content profile, you can use content_profile_load() function, here is my code:

// Get the uid of the user being viewed.
$userdid = arg(1);
// Pass the uid to the content_profile_load() function for that user's profile
$content_profile = content_profile_load('uprofile', $userdid);
$public = $content_profile->field_uprofile_public[0]['value'];
//hide the profile if 'No'
if ( $public == 'No') {
return TRUE;
};

i tried this with cck +

Comment: 

i tried this with cck + content profile but can't get it working... is this part joined in anyway to the snippet above

also, i'm assuming the cck field you created is called uprofile_public

could you show the entirety of your code snippet?

thanks

Excellent! Thanks! You also

Comment: 

Excellent! Thanks! You also may want to make it so that certain roles or permissions will allow the display of the profile, but this is a great starting point!

Do you know why it would be

Comment: 

Do you know why it would be that anonymous users cannot actually see the Panels? Instead of the normal Panels being displayed, these steps seem to result in panels never getting displayed for anonymous users, and the standard account page is displayed instead of the Panel override? I cannot get this to work as it should.

I'm sorry, but this is just horrible

Comment: 

I'm sorry, but this is just horrible instructions. You should explain it in more details like how to overwrite user profile page and how to find the fid number in the database!

Thanks for giving this post.

Comment: 

Thanks for giving this post. This is really a good post. I like this so so. I have searched in Google for this information and only your site has helped me to obtain the results. Thanks for sharing it with us.

Update for D7

Comment: 

In Drupal 7,
In Drupal 7, here's the PHP Code to add as a Panels selection rule to test if a profile checkbox is set:

<?php
$public
= field_get_items('user', user_load(arg(1)), 'field_public_profile');
return
$public[0]['value'] == '1';
?>

Note that:

  1. This expects that the arg(1) is the user being viewed.
  2. This expects that your user has a profile field named 'field_public_profile'.

Adjust as necessary.

Add new comment