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