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 blockUsing 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:![]()
Now you'll be asked to name your view something computer-friendly - in this case you'll name it related_by_term:![]()
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.
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:![]()
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:
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:
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:
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:![]()
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:![]()
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:
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:
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:
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:
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):
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
Pingback
[...] The second one was a question delivered to me via chat: How to create a “related pages” block in Drupal 6, but we can’t find any useful modules and my personal recommendation, Panels, is not ported to version 6 yet. Therefore, with some Arguments magic, I bring you (now, new and improved, with 15 screenshots!): Using Views 2 and Drupal 6 to Create a Related Pages Block [...]
Pingback
[...] The second one was a question delivered to me via chat: How to create a “related pages” block in Drupal 6, but we can’t find any useful modules and my personal recommendation, Panels, is not ported to version 6 yet. Therefore, with some Arguments magic, I bring you (now, new and improved, with 15 screenshots!): Using Views 2 and Drupal 6 to Create a Related Pages Block [...]
This is a fantastically done
This is a fantastically done Views 2 tutorial. Thank you for writing it!
BTW folks, the above comment
BTW folks, the above comment is by the guy who WROTE Views 2...
Thanks, Earl!
I hate to be "that guy" but
I hate to be "that guy" but any comment on how this would be done differently in Drupal 5?
wouldn't really be that
wouldn't really be that different in Drupal 5, but the Interface to Views is COMPLETELY different, so none of the screenshots apply here. You would still use the same code as your default argument, still use the same fields and such.
This is great Ryan. It's an
This is great Ryan. It's an exceptional demonstration of your expertise along with your willingness to help Orlando techies. You're a true community leader!
Excellent, I'll be using
Excellent, I'll be using this
Hi, first of all, thanks for
Hi,
first of all, thanks for the great tutorial!
Unfortunately, I can't get it running under D6 (PostgreSQL) - are there any problems with PostgreSQL?
I have followed the turial step by step and get a error as soon as I inserted the php and press update.
Basically, I want to use your basic idea to create a blockview that, in addition to the taxonomy/term view,
displays all nodes with the same tid and then use filters to decide whether the nodes (a vocabulary that organizes the area where they are displayed) are displayed with the content-area or inside the block.
In D5 I supplied the argument within the URL - this worked for Blockviews as well in D5.
I have spent hours but somehow I don't get this rather easy setup running in D6..
Any help is appreciated!!
Hannes
Pingback
[...] right now to see if I can potentially use an argument to do it. To review the site click here. Technorati Tags: Using Views 2 and Drupal 6 to Create a Related Pages Block Bookmark and Share: [...]
This is exactly what I was
This is exactly what I was looking for. Very nice tutorial and an elegant solution. Thanks for sharing!
sucked
sucked
I don't seem to be able to
I don't seem to be able to match the new content type I created and the views. I had created a view, to which I added the fields that I needed from the content type I had just populated with some dummy data, but all I keep getting is a blank table. Not sure what I am doing that is wrong. I must have spent hours on this, still cannot resolve it.
I hope I can proceed, as this is a big stumbling block for me. I am hoping to create content types and views, as it is a wonderful modules.
Good article. What if (as
Good article. What if (as seems to me would be more common) you have a page whose main content is generated by a View, and that view also has blocks defined? Seems to me that Views should have an option to indicate that the blocks for the view should go into particular regions on the same page, take the same args as the page (or at least, same args as default), and output cleanly. Maybe that's the whole idea of Panels, but it seems like Views is already tantalizingly close.
Can you offer a code snippet for Default Arg, similar to above, for grabbing from the URI, consistent with the main page that specified foo/% in the Page URL? It seems like it would be an even handier capability.
Just one thing... First,
Just one thing...
First, thanks for the tutorial. The only thing I ran into in creating the related-pages block with views is that the block appears ABOVE the other blocks in the region, EVEN THOUGH I POSITION THE RELATED-PAGES BLOCK BELOW THE OTHER BLOCKS FOR THE REGION in the Blocks admin section.
Is there a tweak that will fix this? Do I need to go under the hood and access a theming function, for example, to position this at the bottom of the region?
Or, is the only way to position a block created in Views to create a special region exclusively for this block and position the region in page.tpl.php where the block should appear?
Thank you for your assistance. And again, nice tutorial!
Thanks a lot for this
Thanks a lot for this tutorial. This pointed me in the right direction. Since I only want to consider a specific vocabulary with only one term by node I modified the PHP code as follows:
<?php
$node = node_load(arg(1));
if ($node) {
$terms = taxonomy_node_get_terms_by_vocabulary($node, 1);
return $terms[1]->tid;
}
return false;
?>
There is one problem that remains. The node I am currently viewing also appears in the related block. I want to exclude it but don't see how. Any ideas?
I just found a solution. I
I just found a solution. I added the node id as a second argument, selected Node ID from URL as Default argument type and checked Exclude the argument .
Views is great and even greater, if you know how to use it ;)
Post new comment