Tutorial: Creating a simple one user = one profile page custom profile

Last updated on
30 April 2025

UPDATE JUNE 6: I had forgotten about this handbook page. My original plan was to redo this page to take into account the changes in nodeprofile's latest version. Instead, I ended up writing a tutorial on a pretty feature filled user profile which you can see on my site (this is a broken link).

As for this page, it's still applicable for versions of nodeprofile before April 29. As of the April 29 release, there are new features that make it easy to add the profile to the user view page and also to the registration screen. It's worth just reading the module docs and trying it if all you want is a super simple profile.

*** END UPDATE NOTE ***

This tutorial will walk you through step by step in creating a customizable user profile using the NodeProfile family of modules. The resulting profile is one node and does not make use of the most advanced features such as multi-page profiles using pageroute.

There are two node types involved in this tutorial:

Usernode - This node type is created by the Usernode module. The module takes care of automatically generating one usernode per user. This node contains no content and only serves to link a user to a node. You can find out more about this in the Usernode readme.txt file.

User Profile - This node type is created by us using CCK. When we create this node type, we tell the Nodeprofile module that this is a nodeprofile node. These nodes are not automatically created and must be created for each user. In the tutorial, we will take this node and embed it inside of the usernode.

Gather the required modules

Download and install these modules: usernode, nodeprofile, nodefamily, cck. Read the readme.txt files of the first 3 carefully as they will help you understand what each piece does.

Make your user profile content type

Create a new CCK content type. Name should be "User Profile" and Type should be "uprofile". (You can use different names, but you'll need to make changes throughout the tutorial.) Delete the text from "body" unless you want to use that field. Check the box labeled "Use this content type as a nodeprofile for users" and leave the "Maximum population" at 1.

This is the node that will become the profile. You can add any fields to it that you want, such as fields for stats (age, gender, etc), fields for interests (books, tv, movies, etc), and more. Exactly how you customize this node is up to you.

Bind the usernode and the User Profile

The usernode module automatically creates a usernode for every user, but it does not automatically create a User Profile (nodeprofile). Once the User Profile is created (more on that later), we need to bind the two nodes together to create the effect of having an automatic profile for every user. The checkbox you did in the last step associates your User Profile node type with the usernode. Next, we need to force the automatically created usernode to display the User Profile (if one exists). To do this, create a new text file named "node-usernode.tpl.php" in your theme dir. Paste into this text file the following code:

  // Redirect the /usernode link to the actual usernode so we get the tabs on top
  if (arg(0) == 'usernode') {
    drupal_goto('node/'.$nid);
  }
  
  // Load and display the User Profile, if there is one, otherwise display a message
  $children = nodefamily_relation_load($nid);
  if (empty($children)){
    print "This user has not yet created a profile.";
  } else {
    print node_view($children[0]);
  }

Once this code is in place, you can go to www.example.com/usernode and it will load up the usernode for the currently logged in user. Inside of the usernode is your custom User Profile, if there is one. If the user hasn't created a profile, the message "This user has not yet created a profile." will be displayed instead.

Make the edit tab work

If a user looks at his own usernode, there will be a "view" and an "edit" tab at the top. At this point, the "edit" tab tries to edit the usernode and not the User Profile that we stuck inside it. There are various ways of fixing this, including getting rid of the edit tab and putting the link on the menu instead, but I used the following hack for to make the edit tab work:

Open up your page.tpl.php in your theme and find where it prints out the $tabs. Themes vary, but it should look something like this (but all on one line):

if ($tabs):

print $tabs

endif;

Immediately above that line, put in this code:

// This bit fixes the edit tab so it goes to the nodeprofile instead of trying to edit the usernode
if ($node->type == 'usernode') {
  $tabs = preg_replace('/\/node\/[0-9]+\/edit/','/nodefamily/uprofile',$tabs) ;
  $tabs = str_replace("Edit","Create or Edit Profile",$tabs);
}

What this does is take the "/node/##/edit" path in the tabs and replace it with "/nodefamily/uprofile". This is a special URL that takes you to the edit page of the User Profile of the logged in user. The handy thing is that this link will also take you to the create page if your profile doesn't exist. So you have one tab that does both, which makes it easier for the user. To make this clearer, the above code also changes the name of the link from just "Edit" to "Create or Edit Profile".

Note: If you have edit rights on nodes created by other users, this link will be wrong for everyone but you as it goes to the logged in user, not the user that goes with the node. For users who can only edit their own nodes, this isn't an issue, and so this hack works as long as any of your super users are aware of it. If you want to edit someone else's profile, you need to go to "/nodefamily/uprofile/#UID" where #UID is the ID of the user you want to edit the profile for.

When a user makes a post, their name on the post is automatically linked to their account info. If you would rather have that link take you to their user profile, you can use some code that comes with the usernode module to fix it. Look in the module directory for the file template.php. If your theme doesn't already have one, you can simply copy the file. If you already have one, you'll need to copy the contents of the file into your existing one. You should just be able to put it at the end as long as your existing template.php doesn't use the same functions. Once you've done this, go back and click on the person's name and their usernode (with the embedded User Profile) will come up.

Further thoughts

You now have an automatically created usernode that will show the person's profile once it's created. You can enhance this with normal CCK node theming methods. Because of usernode, you can also use views to search profiles. This tutorial only covered one simple case. Check out the rest of the docs for advanced usage.

Help improve this page

Page status: Not set

You can: