Skip to: Site menu | Main content

Author Biographies on Nodes Using the Content Profile and Views Modules in Drupal 6

Collecting and using user profile information has always been a popular aspect of the Drupal module scene. The Profile module (part of Drupal core) has always been a relatively straight-forward way of collecting additional profile data about users, but its lack of default Views and CCK integration has been problematic for most users.

Saving user data as nodes has been possible using a variety of methods for quite a while, but it seems that with Drupal 6.x, things are coalescing around the Content Profile module. This allows you to set a particular CCK content type as a user profile (the module actually creates a default "profile" content type automatically for you) - thus gaining all the advantages of CCK and Views (and their associated universe of modules) when dealing with user profile data. This is extremely powerful and lets you do all sorts of wacky things with your user's profile data (don't be evil).

This article talks about the (relatively easy) process of getting the Content Profile module configured for a Drupal 6.x site. Then, I'll go through the process of making one of the profile fields available to Views and a template file for use when displaying a node. This might be useful if one of the Content Profile fields you're collecting is a short biography of the user that you want displayed within any nodes the user has authored. Then, your standard node view can look like this:

Short Biography display

The first step is to download, install, and enable the modules you'll need:

  • Content (CCK)
  • Text (part of CCK)
  • Content Profile - I used the dev version from 9/29/08, as the beta2 version wasn't working properly with Views
  • Views
  • Views UI (part of Views)

When the Content Profile module is installed, it will automatically create a "Profile" content type. You can go to "admin/node/types" to confirm this. Clicking to "edit" the Profile type, you'll see that this is a standard CCK content type with one exception - near the bottom of the edit page, you'll see a "Content Profile" fieldset. Inside the fieldset, you'll see a single selected checkbox: "Use this content type as a content profile for users" - this basically links each node of the Profile type to a particular user automatically with automatic integration on each user's "My account" page.

Content Profile checkbox

In the "Submission Form Settings" fieldset, let's go ahead and set the Profile's title to hold the user's full name. Just change the "Title field label" to "Full Name". Let's also change the "Body field label" to "Short Biography" - this is the field we'll eventually have displayed under the byline of the Story content type:

Content Profile field labels

In the "Workflow settings" fieldset, uncheck the "Promoted to front page" box.

If you have the comment module enabled, in the "Comment settings" fieldset, set the "Default comment settings" to "disabled".

Once these changes are made, let's save the Profile content type.

Clicking to "edit" the Profile content type again, you'll see that there are some additional Content Profile settings under the "Content Profile" tab - we'll leave the default settings for now. One complaint I have about the "Content Profile" module is that even when the "User page display style" is set to "Display the full content", the "title" field (or the "Full Name" field, in this example) is not displayed on the "My Account" page. I'm not sure if this is a bug or the module was designed this way.

Profile Title not displayed on My Account page

Like any other content type, you can add additional fields to your heart's content - we're going to keep things simple for now and go with what we have.

Next up, we need to visit admin/user/permissions and give "authenticated users" permission to "create profile content" as well as "edit own profile content" and "delete own profile content". Feel free to modify this step to meet the particular needs of your site.

You can check out what we have so far by clicking on the "my account" link in the main menu. Since all of this is brand new, you won't have any profile data entered yet, so you'll see a "Create your Profile" link. Clicking this will bring you to a standard Drupal node form where you can enter your Full Name and Short Biography. Go ahead and fill these out before continuing.

So, at this point, we have the Content Profile module up and running and collecting user data. You can also enable the "Content Profile User Registration" module (it is included with the main Content Profile module) to collect the profile data during user registration, much like the standard Profile module does.

Now, we want to set up a view that displays a list of nodes along with a field that displays the author's "short biography". To start, you'll need to take a few minutes to create 2 or 3 "story" nodes (be sure the "author" of these nodes is the same user as you used when entering your content profile data). Go ahead, I'll wait here.

Ok - all set? Good. Let's go to admin/build/views and click to "Add" a new view. Set the first set of form fields to:

Content Profile view

If you haven't used Views 2 before, this next part might be tricky (check out these screencasts for a quick primer). First, let's set up the "Defaults" part of the view:

  • Add Filter: Node: Published = Published
  • Add Filter: Node: Type = Is One of Story
  • Add Field: Node: Title (link this field to its node)
  • Add Field: Node: Teaser
  • Add Field: User: Author (Label: Author)

At this point, in the "Live Preview", you should see your test nodes appearing. Let's go ahead and add the author's short biography. This is done using a Views "Relationship":

  • Add Relationships: Node: Content Profile (require this relationship, select the "Profile" content type)

This sets things up so that the author's content profile is linked to the story node via the user id (author) of the node. The next step is a bit tricky. We need to add the "Node: Body" field - but not the "body" of the story node, we're actually going to add the "body" (which is our "short biography") of the Profile node via the Content Profile relationship we just added. Click to add a field, then select the "Node: Body" field - after you click the "Add" button, you'll be prompted to configure the "Node: Body" field. This is where you select the "Content Profile" relationship, telling views that you want the "Node: Body" of the author's Profile node, not of the Story node:

  • Add Field: Node: Body (Relationship: Content Profile, Label: Short Biography)

Click the "Update" button and Voila! Checking out the Live Preview, you'll see that Short Biography for the author of each node is now listed. You may want to adjust the order of the fields using the up/down arrows in the "Fields" section. Go ahead and save the view at this point.

Let's go ahead and add a "Page" display for this view. Click the "Add display" button (assuming that the associated select box is set to "Page"). Under "Page settings" set:

  • path: contentprofiletest

At this point, your "edit view" page should look something like this:

content_profile_test view

Click to save the view, and then go to: /contentprofiletest to see the results so far.

The last step is to make the "short biography" field available to the appropriate node template file so that when a user clicks to view the full node, the "short biography" is still visible. Right now, clicking on a node's title brings us to a standard node page, but without the "short biography" field. The reason it isn't being displayed is that the "short biography" field isn't part of the "story" content type - it is actually part of the "profile" content type, so Drupal doesn't know to include it as part of its node loading behavior. We'll have to write a small module to accomplish this.

I'm only going to show the "meat" of the module within the article, but you can download the entire module below. Basically, all we need to do is add the "short biography" field data to the story node's data whenever the story node is loaded (we could also do this using the "view" $op, but using the "load" $op gives us more flexibility if we want other modules to be able to modify the Short Biography data as well). We'll do this using hook_nodeapi():

function anelloconsulting_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'load':
if ($node->type == 'story') {
$author_info = content_profile_load('profile', $node->uid);
$node->short_bio = check_markup($author_info->body);
}
break;
}
}

The Content Profile module provides the handy "content_profile_load()" function that gets the data based on user id - we simply pass it the name of the profile content type ("profile", in our case) and the user id of the author of the node. Then, we set a new "short_bio" field to the node that is equal to the "body" field (our "short biography") of our profile node. If we had additional profile fields that we wanted to add to the node object, we could add them in a similar manner.

At this point, you can now access the author's short biography in your template file using:

<?php print $node->short_bio; ?>

You can quickly test this by altering your theme's node.tpl.php (or node-story.tpl.php, if you have one) to include this new variable.

I'm fairly certain that you can use the Panels module to override the default Drupal node display to include the Short Biography data within the full node view as well, but that exercise is for another day.

AttachmentSize
anelloconsulting.zip1.81 KB
Submitted by michael on Mon, 11/03/2008 - 6:12pm
Filed under:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Where is this field?

You're step for creating a view has

Add Field: User: Author (Label: Author)

But I see no such field. And I re-read the post and found no mention of creating such a field.

core field

Sreve,

It's a core Drupal field - you'll find it under the "User" section when you click the "+" to add a field. I think it may be named "User: Name" now, but it's the same thing.

-mike

Using this w/ custom fields?

This module is really saving my life -- thank you! Unfortunately, I'm hitting a snag... I'm trying to get some custom profile fields to display. So, for example, I am trying to replace:

$node->short_bio = check_markup($author_info->body);

with

$node->short_bio = check_markup($author_info->field_institution);

but when I do this I get an error... is there something I'm missing?

Thanks again, so much!

Hiding the title field

I think the title field was supposed to be hidden, but it shows up when you edit the profile. Try using the Automatic Nodetitles module. That will allow you to hide the title field even in the edit mode. Hope that helps somebody. Seems to work well for me.

Great article!!! It helped me

Great article!!! It helped me a lot!!! But I got puzzeled following your last step, I don't know how to use your final module whose download link is not listed in the article, do I just add that module and enable it as other modules and modify the node.tpl.php file by adding 1 lines?
I added more fields in the profile content type, such as "Gender","Age","Industry", how can I configure the views 2 to display them,too?( There is "Body" type node but no"Gender" type node, I got stuck in configuring the fields of the view).

Thanks a lot!

whoops!

Heh - I forgot to attach the module when I first posted the article. It is posted now. Check it out and let me know if it answers your questions...

-mike