How to pass arguments to Views Block D6

As you may know by now, the Views module is a complex and powerful application that opens up a world of possibilities really. Using it in the last project, I was confronted with a dilemma, creating a simple, dynamic block that displays a link to a node's profile page. Here's a quick breakdown of the task at hand:

This tutorial assumes you know your way around Drupal and you're well acquainted with the Views module.

1. We have say, a content-type created called “destination” that lists information about a destination like place, climate, and other tidbits of info.

2. Create a view that gathers the info you want presented at a dynamically generated URL. For example, I created a Views page that accepts a node argument via the URL destination/%/profile. On this page, I list everything about the destination's profile so I have included all the cck fields I added to the content type like temperature, history etc.

3. You may go ahead and add filters to only show certain nodes that match your chosen criteria. Published nodes, node type = destination, etc

4. At this stage save the view. If you go to a node that you created for our content type and grab it's node:id (nid) , you can add that argument on the Views page and see it output the data for fields you added (in a bit lol)

5. Now here's the tricky part, we need this info to be presented in a Views block right? Go ahead and create the block.

Views blocks don't take arguments, from my research, they seem to have a very limited sense of context. SO what you do is you go the arguments section, add an argument for nid (node id). Navigate down and choose Provide Default argument -> PHP Code

In the text area you have to put the following:

<?php
$path = $_GET['q'];
$path = explode('/',$path);
//let's grab our node id now
if($path[0] == 'node' && is_numeric($path[1])){
return $path[1]; //the nid!
}
?>

We are done! The nid is now exposed. I've been told by a friend that the node is indeed accessible to Views by simply picking Node Id from URL as default. Whilst I can't validate this claim, I don't think it hurts to know the techy way of doing this and how stuff works under the hood!

If you have any questions, please post a comment and I can clarify the process. Thanks for reading!!

Add to Google

Tweet About This!

About Chiko

I'm a web developer who loves learning, creating, sharing and talking about technology. A nerd. @mukwenhac on Twitter!

Chiko's picture