skyred 发布
Goal
Create a checkbox option to let individual user to hide/un-hide their profiles on a Drupal site (Drupal 6).
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.
2. Use Panels3 to overwrite User Profile page (/users/%). Then create two variant displays for two kinds of conditions above (If ... then ... else).
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
评论
jeff 答复于 永久连接
great work, is there a way to
great work, is there a way to intergrate with user relationships in your code? i'm using core profile
Thanks
Adam b 答复于 永久连接
I'm having trouble finding
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
Anonymous 答复于 永久连接
If you are using Content profile
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;
};
Anonymous 答复于 永久连接
i tried this with cck +
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
pauldawg 答复于 永久连接
Excellent! Thanks! You also
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!
pauldawg 答复于 永久连接
Do you know why it would be
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.
Rob Loach 答复于 永久连接
Well done!
Well done!
Anonymous 答复于 永久连接
I'm sorry, but this is just horrible
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!
google 答复于 永久连接
Thanks for giving this post.
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.
Peter W 答复于 永久连接
Update for D7
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:
Adjust as necessary.
添加新评论