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
Comment #1
dawehnerI don't find the question :)
Comment #2
dawehner.
Comment #3
esmerel commentedNot sure what problem is being solved here; feel free to write a doc patch and submit. Until then, closing the issue.
Comment #4
bartezz commentedLivesaver!!!!
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;
Thanx for sharing!
Comment #5
ashhishhh commentedThank you :)
Comment #6
hans0811 commentedThanks 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.