I'd like to add a simple tutorial to the documentation for views 2. I spent quite a bit of time finding the answer to this one, although it's really quite simple once I found it.

Basically, I wanted a quick way to get at the views arguments within PHP code in the Header/Footer/Empty text, etc... I found lots of unanswered questions in various forums from views 1 users that could use the "$args[x]" format.

I found the new way to accomplish the same thing is to do the following:

In Views 2 the arguments are stored in an object that you have to get with a call to views_get_current_view(); An example would be as below:

// The first argument to my view is User: Uid
// I'm going to print X if it is the same as the current user, and print Y if it is not
global $user;
$view=views_get_current_view();
$arg1=$view->args[0];
if ($arg1 == $user->uid) {
  print "X";
} else {
  print "Y";
}

Thanks!

Comments

dawehner’s picture

I don't find the question :)

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

Not sure what problem is being solved here; feel free to write a doc patch and submit. Until then, closing the issue.

bartezz’s picture

Livesaver!!!!

I was trying to set an empty text like such;

print "No content found tagged with: ".taxonomy_get_term(args(7))->name.", we are sorry";

This worked nicely when previewinig the view in the views UI... but not when viewing the node via viewfield (the way I've implemented the view in a node).

But this works;

$view=views_get_current_view(); 
print "No content found tagged with: ".taxonomy_get_term($view->args[0])->name.", we are sorry";

Thanx for sharing!

ashhishhh’s picture

Thank you :)

hans0811’s picture

Thanks Tim! This was exactly what I was looking for!
I used it to display a link in the footer to the full view only in case the view is reduced by an argument.