How to Build a Drupal Social Network
Part 2: User Profiles
Use Drupal Nodes as The User Profile
For the most flexibility, we'll use the typical Drupal 6 development work flow of Content Construction Kit (CCK) and Views to make the user profiles on our social network. Make sure you have a solid understanding of hows Nodes, Content Types, and Views relate to make the basic components of a Drupal site before attempting this.
Modules Used
Advantages of Using a Node as a User's Profile
- Field configuration / flexibility: By using a node as the user's profile, we're able to use all of the features that come with the Content Construction Kit (CCK). Most notable are things like role-based field permissions, multiple types of profiles, and being able to take advantage of CCK add-on modules like imagefield crop.
- Data Presentation and Integration with Views: Because of the way drupal stores data and the way views functions, you'll have no problems trying to aggregate data from profiles and other types of site content.
1. Set up the Profile Content Type
1. Download and Install the Content Profile module: http://drupal.org/project/content_profile
2. Create the new content type: call it "profile".
3. Check "use as profile" in the content type settings.
4. Configure your fields: It's a good idea to make a croppable profile photo, change Title to be "Full Name", etc.
5. Generate some dummy acounts and profiles so you can get a feel for how everything works by default.
Creating a Unified Experience: Redirecting the Profile to the User's Page
One weird thing that this model produces is 2 separate Drupal pages represneting the user. You have the user's profile node at http://www.mydurpalsite.com/node/[nid], and you can see it when you visit /node, it shows up in search results, etc. You also have the user's account page at http://www.mydurpalsite.com/user/[uid]. The users see their account page when they log in, and depending on your specific site needs it could be strange for your users to have both.
The easiest solution is to redirect the node profile (http://www.mydurpalsite.com/node/[nid]) to the account page (http://www.mydurpalsite.com/user/[uid]), and work on themeing the account page. You can do it in reverse, but the process is very involved.
Redirect Option #1: Rules Module
If you have a solid understanding of the rules module, you can create a condition to check the type of node being visited, and do a redirect to that node's author.
Advantages: Great if you feel comfortable in rules.
Disadvantages: If you don't know how to use rules, this one has an awkward learning curve.
Redirect Option #2: Implement hook_node_api
If you've had some experience writing Drupal modules, this one's cake. Make a new module and implement hook_nodeapi to redirect the view and edit options to their corresponding location's in the /user tree.
Advantages: Stable & Proper, best practice.
Disadvantages: A bummer if you don't like writing code.
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
if($node->type == 'profile'){
//Get the UID of this node's author
$uid = $node->uid;
//Redirect to the user's page when the node is viewed directly,
//if we just checked the $op without checking arg(1)
//we'd redirect inappropriately.
if($op == 'view' && arg(1) == $node->nid){
//Redirect to the users page
drupal_goto("user/$uid");
}
//Redirect to the user's /edit page when the node is edited directly.
if($op == 'edit'){
//Redirect to the user's edit page
drupal_goto("user/$uid/edit");
}
}
}2. Theme your "profile"
Now we're ready to generate a nice display for the user profile. I usually do this with views.
1. Blank out the default drupal profile by generating an empty user-profile.tpl.php in your theme's "template" folder.
2. Create a new view showing items of type node.
3. Add the User: UID argument and select "User ID from URL" as the default argument.
4. Generate a new block display.
5. Use this method to display fields from your content profile, and assign them to the user profile as blocks. I usually make a block for the profile photo, description, etc.
3. Generate a User/Account Bar
Since the profiles are just nodes, you can . . .
1. Make a new node view
2. Use the User: UID argument and select "User UID from logged in user"
3. Select your fields.
4. Add some extra markup for logout and edit profile.
4. Generate a listing of all users
1. Again, just use views to do this! You can start with a view showing "users" and add a relationship to their profiles, or start with a view showing nodes and add a relationship to the user.
5. Tweak out the registration process.
Personally I absolutely hate Drupal's account activation method where the user has to select their password after registering. If you do too, give loggintoboggan a try.
Posted on November 21, 2010 - 4:49pm









Comments
Add new comment