Mar 11 2010
Mar 11

There are several ways you can theme a view on Drupal, and this tutorial will describe a simple and quick way to accomplish just that. I assume that you’re familiar to working with Views and CCK, so I won’t go in much detail on the process of creating the view and content type.

For this example I’m creating a block simple view that will display a teaser for the 3 latest blog posts.

read more

Oct 06 2009
Oct 06

A common requirement for many of my recent clients has been to move the "Read More" link at the end of a teaser inline to the end of the teaser's last paragraph. Like this:

read more link example

This need usually occurs in a listing, and listings are almost always built with the fantastic Views module. Often on a page showing excerpts of content, sometimes called teasers in Drupal, the displayed data is a small portion of the entire piece and is followed by a link to the full piece of content. This link is often called the "read more" link, for obvious reasons.

When using the Views 2 module there are a few ways to add a "read more" link to listing showing a preview of an article or other piece of content. In the past I used a trimmed version of the body text that was set to a specific number of characters or words and was followed by a "read more" link to the full view of the content. The are more options available now with the second version of Views.  I am going to outline my newest and currently prefefrred hack/technique.

I no longer use a portion of the larger piece of content for my "teaser" rather I use an entirely different field to hold the text to display on my listings page. I know this can create a bit more work with a content type that is a bit larger than it could be and requires the content creator to do a bit more work, but I find the editorial control to be vastly superior. So, typically in my content piece I create a little sentence or two in a field, and that field's only use is to be used as a preview or teaser on my listing page.

 teaser field

Let's look at am example. I begin by creating my view and setting basic settings like "Published" and "Type". I am using a Row Style of Fields. I add a Title Field.  The next thing that is different about this approach is that when I add my other fields to my view I am selecting the option to "Exclude from display" on each of them.

exclude from display
 

In this case the fields I am adding are (each of these has the "Exclude from Display" boxed ticked):

    * the field for the "preview" text
    * a Node: Link field - with "read more" in the "Text to display" box
    * another Node: Link - this time with the "Rewrite the output of this field" box also ticked and in the text area I am adding some markup as seen in the image below

node link rewrite

That little bit in the rewriting text area is the cool bit and is a big part of the final field I am adding to this view.  This trick is a nifty little solution to this problem that worked well for me to solve this problem, but the neat part is the technique.  The way I like to think of it is that I am gathering a variety of information in my view and not displaying it and then I am going to use a field with the "Rewrite the output of this field" box ticked to gather all that information I got back from my Views query and display it using the tokens(aka "Replacement patterns").  This is AWESOME!  I can use bits of HTML in the rewrite box if I need to.  I can also "build up" items in the query to get the token to hold exactly what I want to by rewriting those fileds and adding that to the final rewritten field.  This is one big gotchas to be aware of when using this technique: Beware of the Field Order. If you would like to use the output of one field in the field you are rewriting the one you would like to use must be above the one you are rewriting in the fields sort as seen below:

 sorted fields

 

This brings us to the final step in building up our fields, adding the last field that will hold the rewritten output of all the fields.  For this I chose the "Node: Body" field.  You can see from the image below that I have chosen to  rewrite the output of this fields and I am using the tokens listed under "Replacement patterns" to make the output of the field exactly as I want it.

 rewritten body

This is a huge win for me as a front end developer.  Now I have the pieces of my View wrapped in the elements I want them in so that I can easily use a little CSS to get things to display just how I want them.

In the time I have been using Drupal I have learned that there are many different ways you can solve the same problem. One of the reasons I find this technique useful is that it makes sense to me as a front end developer and gives me the flexibilty I need in a way I understand. 

If you find this helpful or have different way you approach a preoblem like this please leave a comment.

Nov 03 2008
Nov 03

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.

Attachment Size anelloconsulting.zip 1.81 KB

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web