Community Documentation

Views Arguments basics

Last updated September 29, 2010. Created by arbel on October 21, 2009.
Edited by xenophyle, kpander, madlee. Log in to edit this page.

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 : Taxonomy: 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->display_handler->display->display_plugin == '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->display_handler->display->display_plugin == '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
->display_handler->display->display_plugin == '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
return $args;
?>

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.

Comments

comparing term names from two different vocabulary

I think I watched every Views Argument video and postings we have -- but can't seem to locate good information on setting up a View comparing Term Names from two different Vocabulary…. all most there but yet so far away :)

Here's what I've done so far

Goal: only see Titles from Vocabulary 4 in a block when viewing Node which has an exact same term name but in a different Vocabulary used by a different content type.

Note: Vocabulary 4 and 17 are used by different content types

Created a Block Display

Configure Relationship Taxonomy: Related terms
1) Gave it name
2) Set up a relationship between Vocabulary 4 and 17
3) Checked box = Required this Relationship

Configured Argument
1) Taxonomy: Term
2) Use Relationship
3) Deleted both "ALL" in wildcards
4) Validator options = Taxonomy Term
a. Selected Vocabulary 4 and 17
b. Argument type = Term name or synonym choice
c. Transform spaces to dashes

Added a Filter
1) Require relationship selected
2) Is one of Vocabulary 4

Fields
1) Title
2) Terms (for tesing only)

Well the filter is working - lol - But seeing all Terms when viewing the Node.

Is anyone able to see what I'm missing?

I think I made a tricky choice for my first Argument and Relationship.

-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
"I would love to change the world,
but they won't give me the source code"
-JG

I think i know the problem,

I think i know the problem, you're not sending the view any argument, the argument needs to be in the url, or fetched some other way. you need to take the nid that's in the path and convert it to the term you want.

the way i did something similar is this: in the arguments under: Action to take if argument is not present choose - provide default argument.
next in default argument type choose php code

I used this code

//checks to see that the url format is /node/nid
if (arg(0) == 'node' && is_numeric(arg(1))) {
//load the node variable based on the nid
$node=node_load(arg(1));
//get the terms from the node in vocabulary id 6
$term = taxonomy_node_get_terms_by_vocabulary($node, 6);
//return the term you want as the arugment
$args[0] = key($term);
}
return $args[0]

I haven't worked with different vocabualries and relationships but this is the basics.

good luck

I can't

I did all thing like your instruction but I can not filter some node which is the same term with current node.
Here is my SQL I get from Query:
SELECT node.nid AS nid, node.title AS node_title FROM node node LEFT JOIN term_node term_node ON node.vid = term_node.vid WHERE term_node.tid IS NULL

I only see a list of some nodes which are not belong to any term.
Who can help me?

I think some of the code

I think some of the code mentioned above referred to Views 1. Specifically, replace:

$view->build_type == 'block'

with
$view->display_handler->display->display_plugin == 'block'

For me that got it working with Views 2 / Drupal 6. I've updated this page to reflect the change.

Kendall Anderson
Abandoned Industry | www.invisiblethreads.com

I have a related problem which I'm looking for some help with:

Say I have a content type which I'm using to display info for individual cities, so I'll create one page for 'London' and one for 'Paris'. The content type has fields associated with it, say 'Country' and Language', which may or may not be linked to a taxonomy term.

Now, I'd like to gather these fields in a view to place next to the body copy, but when I create the view it shows all values for the 'Country' field and all values for the 'Language' field, so Iget something like this:

UK
France

English
French

If anyone oculd explain how I might ensure that only the field content that is actually part of a particular story appears in the view, I'd hugely appreciate it.

Ta

Location

This is all nice, but where do I put the code?

dvereber, I am still learning

dvereber,

I am still learning this myself, but I believe this code goes in a View's argument, where you choose the "Validator" to be "PHP Code". For instance, if you add a "Node: Nid" Argument to a view, before you update the default display with this argument, look for the Validator options section to find the "Validator" drop down select box.

BigMike

Passing argument to filter

Anyone knows how to pass an argument to a filter?
Strange question?
Well argument are set diferent.
For example in filter I can use "equal" or "contain" or "less"
But in arguments not.
All I need is to check if an argument is a word contained in an text field where several words are.
Arguments usually check if passed $arg is equal to a field but not contained - don't know how to solve it.

Regards

hello drupal world!
my drupal site: Wyprzedaż, promocja, przeceny

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders, Documentation contributors, Site administrators, Site users, Themers

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here