Using Views 2 and Drupal 6 to Create a Related Pages Block

Published June 17, 2008

Today's question comes from Dale at NFi Studios in Orlando, FL - my home town:

Essentially:, what i'm trying to do is
1. Determine the current nodes taxonomy terms
2. Determine all other pages that share taxonomy terms
3. Display the title (and link) to those pages in a block

Using Drupal 6.2 and Views - Looked at a few modules, but nothing quite
exact - Reviewing some module snippets right now to see if I can
potentially use an argument to do it.

Dale asks a good question: before the release of Panels for Drupal 6, how can one associate a block with the node it's being displayed next to without writing a custom module? It turned out to be simpler than I expected. I actually started going down the path of custom modules, but in the process I noticed that Views still have the ability to load Arguments with PHP Code, and my solution wrote itself.

Dale, you're already pretty far along: you know you need a view inside of a block, displayed based on taxonomy terms to relate the view to the pages. Instead of giving you a full-fledged tutorial on Views and the Black Arts, you'll be getting a recipe that outlines how to create the block in question.

First, you'll want to download and install Views 2 for Drupal 6, currently in Beta 4, then enable the module under admin/build/modules - remember that Views also requires you to activate Views UI, as the views.module is more of an API for creating views.

The first step in your new Views journey is to Add a new view at admin/build/views. Just click on the tab near the top of the screen:
add a view

Now you'll be asked to name your view something computer-friendly - in this case you'll name it related_by_term:
Name your view

Once you click through, you'll be presented with the granddaddy of all administration screens, but don't be afraid, things move pretty quickly in here if you know what you're doing. Notice the Orange text in the top right: New view. Until you push the Save button at the bottom of the screen, all you hard work could be lost, and especially don't forget to save once you've finished configuring your view.
Main views screen

I want you to skip all those fancy options and dive right in: you've got to eat your vegetables, so let's get the hard part over with and set up the arguments:
Add an argument

Arguments were the most powerful (and hardest to understand) feature of the old Views - think of this as the WHERE clause of your SQL statement. Your first and only argument will be keyed off of a Taxonomy Term ID:
taxonomy argument type

Since you don't have Panels at your disposal, you'll need to specify a Default Argument. There are several (powerful) ways to accomplish this, but in your case, resort to tried-and-true PHP Code. The code displayed here loads the object for the current $node (if any) and concatenates a list of all the Term IDs on that node, then returns the string to Views in the format 1+2+3, just like the arguments on taxonomy/term pages:
use PHP to specify the default argument

In order to activate this muitliple-term-driven argument scheme, you need to check a few boxes below the PHP code. The first sets up the fact that you can use the 1+2+3 format, and the second removes duplicate entries that have more than one term. Make sure you read the warnings about performance; if you don't use multiple taxonomy terms, leave this box unchecked. You can proceed by clicking the Update button:
Allow multiple terms, reduce duplicates

At this point, you'll see an error message inviting you to extract some fields in your view, which is set up to display fields by default. If you wanted to view full nodes or teasers, you could change that option and be safe, but the block you want to build needs titles that link to the node:egads, an error message

Now you want to tackle adding fields to your view. This is the building block of any view, analogous to the SELECT part of a SQL statement:
Add a field

You should recognize this next picture, because it's very similar to the way you chose your argument type, and how you'll choose your filter in the future. Use the Add button to move to the next screen:
you want to see the node title

If you don't delete the word "Title" in the Label text box, it will appear next to all your links. There are some situations where this is wanted; yours is not one of them. You also said you wanted the titles to be links, so check that box:remove the Label and enable links

At this stage, you should be able to get a working preview of your view. Just type a number or a 1+2 in the arguments area and push the button to trigger the AJAX. That blue text is your view, and there's also some geeky information below. Notice that currently "This display has no path"; that's OK, you want a block:
use some arguments to get a live preview

At this point, you still haven't told Views that you want your view to be a block, so make sure you choose block from the drop-down in the main interface. If you're working strictly with Page nodes, you may also want to throw a filter on your view to limit the results. You also should never forget the all-important Save button:filter and save your view

The last thing you need to view is activate your block on admin/build/block (you may need to use block visibility rules, but that's another tutorial), and finally visit one of your Taxonomy-tagged pages to see if your other taxonomy-tagged pages shows up. If you've followed all the steps here, everything should be a snap. Maybe the best feature about your new block is the handy links that pop up when you mouse over your new block (if you have administer views access, at least):
your finished block with edit links

Well Dale, I think that should just about answer your question. Since this is my first Drupal 6 tutorial, I'll have to say, this was a lot more fun for me than I'm sure the last few days have been for you. Wave hello to Corey and Sterling and Derek and Daniel and the other NFi folks, and I hope I was able to save you some precious time with this post.

Comments

What if the node belongs to multiple terms?

Hi
This view works greatly when a node has only one term
But if the node has multiple terms the view doesn't work. In my case it shows nothing at all.
i'm running drupal 6.20 and views 6.x-2.12
Is there a way to run this in a multiple terms selection environment?

Again thanks for the good job

Submitted by salim (not verified) on Thu, 01/06/2011 - 09:29

Hi,

I was wondering what needs to be changed to make this work in Drupal 7.

Greetings

Submitted by segmentation (not verified) on Mon, 01/24/2011 - 03:37

@segmentation - the more apt question would be "what has changed in Views 3.x?"

I don't think using Views 3 in Drupal 6 and 7 is very different. Using CCK vs using fields is not very different either. In this example, we are using Taxonomy, which must now be added as a field to any content type as a "Term reference" field.

This would mean that instead of looking for node->taxonomy it's probably more like node->field_tags (where tags is the name of your taxonomy field).

I haven't played with Views Relationships enough to know if this is now possible without writing PHP code in the default for the argument. This is something I always hope will get added. I feel like one of the goals of Drupal is taking coding tasks away from site builders.

Hi,

I realy liked your tutorial... I'm trying to do something slightly different, but maybe you already know the solution....?

I have a table named "rank_user" with uid's en taxonomy id's (uid, tid) now i want to make view, where the logged in user can only see the node's where he or she has the taxonomy id for, you could derive the taxonomy id linked to the user from the "rank_user" table... but how to make a view...?

Thank you very much in advance!!
Regards,
Thijs

Submitted by Thijs (not verified) on Thu, 02/03/2011 - 07:33

Thijs,

Thanks for the comment.

I know our podcast co-host Andrew Riley has developed a similar solution with the UserPoints module - from what I understand, every time a user reads, comments on, or votes for a node, he adds points to a field that relates the user to the taxonomy terms associated with that node, which he then uses as a weighted score.

I'll see if I can get him to help me put up a tutorial for the site.

Hi there. Kinda lost, lol.

What I want to do is show a block only on node with a specific taxonomy term.

For example the term "movie-reviews."

Do I need an php argument for that?

Submitted by Matt (not verified) on Mon, 02/21/2011 - 04:35

Matt,

I'm not sure if you do need PHP to accomplish this, but if you follow the instructions exactly, and then add a "Filter" to only display nodes of a particular taxonomy term "movie-reviews" that is one way to solve the problem.

That's not how I would do it, though.

There is a great module called Context out there. You can set conditions for displaying blocks on pages, among many other things.

Basically, you build a view that shows your term with filters, don't worry about arguments unless you really want/need to.

Next you use context to set a condition that when a node is tagged with the meovie-reviews term, to have a reaction that is to show a block, and then choose the Views block you created. Make sense?

Hey Ryan, thanks for the great walk through. Your instructions works great on D6. However, like in #5, I took am upgrading to D7. I've tried switching $node->taxonomy to $node->field_tags, but that didn't work.

Any advice on how to make this happen in D7 would be greatly appreciated.

Submitted by Bryan Casler (not verified) on Sun, 03/20/2011 - 00:53

Hi, thanks for the tutorial,
but when I try to implement this tutorial, at the end i got this error:

warning: implode() [function.implode]: Invalid arguments passed in \sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(48) : eval()'d code on line 4.

can you help me?

Submitted by Guest (not verified) on Tue, 05/31/2011 - 04:17

This tutorial was written some time ago, when Drupal 6 was still very young - please read some of the comments below for more modern (module based) solutions to this problem.

this is the code for D7

$node = node_load(arg(1));
if($node){
foreach (field_get_items('node', $node, 'field_tags') as $term ) {
$terms[] = $term['tid'];
}
return implode('+', $terms);
} else { return; }

Submitted by enxox (not verified) on Tue, 07/12/2011 - 18:22

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA