Views Arguments basics

First I want to state that this might be incomplete or very simplifying in some ways, but after more than three years with drupal I still haven't completely grasped this.
This page will try to explain in a very simple way how arguments in views 2 work.

First of all what are view arguments?
Arguments are a way to send information to a view, these arguments can filter the view or modify them. I use them mostly to create dynamic view filters, filters that change according to the content that's on the page, usually with block views or views inside quicktabs.

Example:
I have nodes with a certain term, let's say "cars". I'd like to display a block view on each page with other nodes with the term "cars". Kind of a related articles block.

So, I'll add an argument : Taxonmoy: Term ID.
Now since most of pages I'll be looking at will look like this:
www.mysite.com/node/##
I want to take that ## (node id or nid) find out what term is assigned to it (in a specific vocabulary) and then filter my view results according to that nid.

Lets understand the two different types of arguments that are available:
$args and arg() - yes there's a difference.

$args – are arguments that are being passed to the view, so if you have a page view and it has this url
www.mysite.com/pageview

then the arguments are what comes after the page view
www.mysite.com/pageview/first/2/third

$args[0] = "first"
$args[1] = 2
$args[2] = "third"

The second type of argument is the stuff that comes after the website url.
So if I'm looking at a node:
www.mysite.com/node/12

then

arg(0) ="node"
arg(1) =12

so lets take a look at this url again:
www.mysite.com/pageview/first/2/third

arg(0) = pageview
$args(0) = first

Ok, let's backup, we've just added Taxonomy: Term ID as an argument.
if our pages will look like this:
www.mysite.com/node/12

then no arguments will be passed to the view ($args will be empty), but arg(1) will hold 12 the valuable node id (nid).
So under the setting:
Action to take if argument is not present:

which should be always, choose
Provide default argument.

Next we want to manipulate the argument we want to pass to the view (find that term id we want to filter with). Then choose PHP Code.

now this is the code I used, it assumes there is only one term for each node. This is just to explain the basics:

<?php
if ($view->build_type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
$node=node_load(arg(1));
 
$term = taxonomy_node_get_terms_by_vocabulary($node->nid, 1); //change the 1 for the vocab id
$args[0] = key($term);

}
return
$args;
?>

lets go through it one by one:

<?php
if ($view->build_type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
?>

we want to make sure that we're dealing here with a block view so:

<?php
$view
->build_type == 'block'
?>

in addition we want to make sure that the url is what we're expecting that the first argument is 'node' so:

<?php
arg
(0) == 'node'
?>

and that the value after the node is a number:

<?php
is_numeric
(arg(1)
?>

Next we load the node according to the node id we got from arg(1)

<?php
$node
=node_load(arg(1));
?>

Now we'll fetch the term id from the node:

<?php
term
= taxonomy_node_get_terms_by_vocabulary($node->nid, 1);
?>

the '1' is the vocabulary id we're searching in, you can change this according to the vocabulary the term is in.

Finally we give the view an argument to work with:

<?php
$args
[0] = key($term);
?>

we set the first argument to be the term
And we send them all back:

<?php
$args
[0] = key($term);
?>

That's it. Now the view will be filtered to display only nodes with the same term id, as the page that holds the block.

 
 

Drupal is a registered trademark of Dries Buytaert.