Here is what I want to do:
I have a node. Now I want to display in a block next to the node information about the author of the post. This worked fine with views 1. I used PHP Code in the argument:

if(is_numeric(arg(1))){
$uid = node_load(arg(1))->uid;
$args[0] = $uid;
$view->uid = $uid;
return $args;
}

Is this not possible anymore. How would you solve this problem?

Comments

merlinofchaos’s picture

Status: Active » Fixed

In the argument settings, under the 'default action' there is a radio button to set a default value if it doesn't get one. From there you can get to a PHP code box. In Views 2, remember to return just the argument that PHP code is working on, not an array.

rapsli’s picture

Thanks. I though still don't get my head around it:

Argument for User: Uid:

if(is_numeric(arg(1))){
return node_load(arg(1))->uid;
}

When I test now in the little preview (btw. that's way cool). It works as long, as I have a valid uid, say for example for arg = 1 it shows the admin and so on. What should though happen is: arg1 = some nid, which will be converted into a uid.

In this pice of code it shoul just have as argument 1... it just ignores what is in my arguments code.

return 1;
if(is_numeric(arg(1))){
return node_load(arg(1))->uid;
}

Do I have the wrong syntax? Btw: I don't use

Thanks for your help

rapsli’s picture

Status: Fixed » Active

sorry, forgot to change status

merlinofchaos’s picture

return node_load(arg(1))->uid; is not valid syntax in PHP.

  $node = node_load(arg(1)); if ($node) return $node->uid;

Note that the -dev version of Views 2 has some newer 'default' selector to extract node and user from the path so you don't need code for it.

rapsli’s picture

StatusFileSize
new11.94 KB

I think something doesn't work here... the code is just being ignored.
It should be users.uid = '5'? or do I get something wrong?

merlinofchaos’s picture

That code only runs if no argument was received.

If it got an argument, you'll want to use the PHP Code in validation, which lets you transform the argument that came in.

rapsli’s picture

StatusFileSize
new15.77 KB

Oky. I'm going to write a lil handbook page, once I've wrapped my head around this... you'll just have to let me know where to put it. Maybe the help in the module would be a nice place aswell.

Well, here is what I've done.
Even when I just return a normal int, it doesn't react?! Can you just post a lil sample code?

merlinofchaos’s picture

That would be great; submit a .html file as and I can patch it in. It's really easy to add help files, you just have to add an entry in the .ini file and the .html file that matches it, and you can easily test it within Views itself. More documentation == a good thing, and I'll happily accept it!

rapsli’s picture

well, but first I'll have to know how to add these php arguments ;) ... cuz the way it's above it doesn't work. So far I could never get to a point where that php code in the validate function was used.

karens’s picture

I've been trying to get some of this default argument and argument validation working to create a current date default argument and have been going around in circles a bit understanding how the default argument and argument validation code works, and I was using the php argument as my example and getting very confused. After digging into this, I think there is something here that his just not going to work as you intend. Here's my understanding of what the code is doing:

1) If no argument is available, whatever is in 'default argument' will get triggered to return a substitute for that argument. So php code entered in that box will work fine to create a non-existent argument.

2) If an argument is found, validation is run on it. But the result of that is used to set $this->argument->validated, so it expects a TRUE/FALSE result. That's what I see in the validate_argument() function of the views_handler_argument class. This is what the numeric validator does -- returns true or false. The php validator evaluates the code and returns the result, but it is still going to be used to set TRUE/FALSE. So it looks like the purpose of that bit of code should be to check whether an argument should be accepted, not to transform it into something else, and the description on that validator needs to be altered a bit to make that more clear.

So it looks like if an argument is found, but you want to transform it into something else, or override it, there is no way to do that yet.

It's possible I'm mis-understanding something, but that's how it looks to me.

rapsli’s picture

KarenS: This does indeed make sense. I was thinking about this aswell, but was rejecting the thought. I guess a new field would be needed saying something like "Transformation" or "Transform argument".
Merlinofchaos: What's your opinion on that, since you are the architect behind the module?

merlinofchaos’s picture

No, the taxonomy validator does indeed have the power to transform the argument. It just doesn't do it by returning the argument. It has to set $argument->argument if it wants to change it.

rapsli’s picture

I think there is a problem with the $argument->argument At least that's what I'm guessing from the error message. Hope we can get this to work

merlinofchaos’s picture

It appears I told you the wrong thing; I should have said $handler->argument, not $argument->argument. The current -dev version of Views has this information in the description of the PHP Code field.

merlinofchaos’s picture

Status: Active » Fixed

It appears I told you the wrong thing; I should have said $handler->argument, not $argument->argument. The current -dev version of Views has this information in the description of the PHP Code field.

rapsli’s picture

Status: Fixed » Active
$handler->argument = 1;
return 1;

This is probably the most simple version and one could think that the argument will always be 1 -> :(
Oky. I'm going to try with the newest dev version.

rapsli’s picture

I got it finally to work :)

arg(1) doesn't work! Instead use $argument (it's actually written).

@merlinofchaos: As promised I'm going to write a handbook page and would just kinda describe my usecase and how to use the arguments validation on this example. To keep everything clean: Where do you want me to write it? I would make some screenshots, so only text won't work.

merlinofchaos’s picture

If you're able to make a patch, make a help file in the advanced help stuff.

If not, just provide the text and the images and I can reformat it/have it reformatted.

nonsie’s picture

subscribing

rapsli’s picture

Status: Active » Needs review
StatusFileSize
new2.65 KB

oky. I think I got it to work, finally ;)
I attached the little help file for the PHP Validator.

merlinofchaos’s picture

Title: PHP Argument code? » PHP Argument code documentation
Component: Code » Documentation
Status: Needs review » Needs work

I played with this text a little, but it's kind of hard to fit in; what you're explaining is actually fairly specific but the approach you use is going to lead to more confusion than it solves, I think.

rapsli’s picture

I know that it's specific. I wrote it specifically, it was first intended to be for the whole arguments, but I then decided to make it specifically for the PHP part.
Maybe you can just make a little help icon right next to the Argument code. I think it's important to show some functioning code. That's why it took me so long to get it running.
Do you think I should split it up? Generel part and specific part.

ju’s picture

Hello, rapsli,

I read your Using Views Arguments.txt, but I don't understand how can I get argument from url in PHP validate code.

I need Block view for the urls like 'taxonomy/term/23',
how can I get 3d argument from url in PHP validate code of Taxonomy: Term ID argumen?

thank

ivrh’s picture

Is there a way to set default argument to $node->nid. I am not (and can't) passing it via URL but rather need to extract it internally (e.g. the view should get $node->nid for each node being considered to display) - so I could do my own argument validation on the node IDs

merlinofchaos’s picture

"Each node being considered to display".

By what? You can't get node IDs through osmosis, and there isn't a generic $node variable to get that from.

fralenuvol’s picture

Hello merlinofchaos,
I disagree a bit with comment #21.

it is about 1 month I struggle with views 2 and arguments, and finally I understood the basic logic thanks to rapsli.
I think the task to set custom arguments is quite essential, and rapsli's help file contains just the right starting point.

Unless you write a help file ;-) .... I would say this is helpful for many new users starting with views 2.0.

By the way merlinofchaos, thanks for your outstanding contributions!

Tony Sharpe’s picture

I am having a similar problem with trying to use the php argument validator for a calendar view so that I can pass the argument 'this-year' or 'this-day' from a menu system and use the php validator to turn it into the current year or current day. I don't know whether it's a views issue or a calendar issue but if I set the argument to a new value with

$handler->argument = "2008";
 return TRUE;

(the simplest sort of test case) then the title and date navigation of the view use this but the rest of the view will only appear if there is a valid original argument passed in (and it then uses that original argument). I've tried also writing the argument to$arguments and $view->args[0] but that's no different. Further details at http://drupal.org/node/307740
Views 6.x-2.0-rc3, calendar 6.x-2.0-dev(sept 20 2008), date 6.x-2.0-rc3

Bensbury’s picture

I can't understand how arguments work.
I've been following the examples for Context Related Blocks using Taxonomy and some kind of args() php mischief.

$args[0] = arg(1);

http://drupal.org/node/221919
http://drupal.org/node/226036
http://drupal.org/node/140588

However none of it seems to work.
So I plunged into the .api tried php coding things but to no avail.

Then someone also wrote that they can't get those examples to work in Drupal 6.

Then in this thread Merlin of Chaos said the new -dev Views 2 has newer default ways to extract node and user.

And then I became really confused by args() not working now and it being $argument? I'm not sure.

Basically I think Views is great and it looks like making contextual blocks has already been done.

So how can I do this for Views 2, because I'm thinking something has changed and there is a new way to do it in Views 2.

Thanks for any help you can give...

original:
http://drupal.org/node/313143

jeff h’s picture

How about overriding the view title from the "PHP validate code"? It used to be pretty easy in views 1 with something like:

$view->page_title = "My new view title";

However, the $view object in views 2 seems much less straightforward. Could someone give an example of how to do this?

Jeff

okday’s picture

is it possible to extract the term of the current node ad argument with views2??

thanks

Bensbury’s picture

Hey Jeff,

If you are wanting to change the title of the page title when switching views this seemed to work

http://drupal.org/node/383018

If it's not relevant then sorry.
Hope it helps though.

Letharion’s picture

Unfortunately, I'm still highly confused on this subject.

I've tried reading the html document rapsli wrote, but I'm still not any wiser.

This is my default argument: "return arg(1);"
and I pass "3/1" to the preview.
I expect this to yield 1, but I get 3.

What am I not understanding?

micheleannj’s picture

subscribing....

Wow, if the whole $node = node_load(arg(1)); thing is wrong, I've wasted a ton of time... is this documented somewhere that I haven't found in the last week of googling? I've been trying to follow examples such as http://drupal.org/node/370227 and http://drupaleasy.com/blogs/ryanprice/2008/06/using-views-2-drupal-6-cre...

I'm trying to get at CCK fields that are not in the URL or any of the other argument options (eg node id, user id, etc).
For example, I've tried plugging in the following to the Arguments -> Default options -> PHP field:

$node = node_load(arg(1));
if ($node) return $node->field_related_user;

Am I right in understanding from the above that if I have a url like "mysite.com/foo/bar" Views will assume "foo" and "bar" are arguments and so never get to the default argument section?

Help and/or pointers towards more info would be greatly appreciated...

summit’s picture

Subscribing, interested in Instead of $arg(1) use $argument. Still not completely clear to me.
How to get the first argument, or is this the wrong approach with views2. Should per argument been thought?
But then how to use information in say the header of the view-display?

greetings, Martijn

micheleannj’s picture

Just to help anyone facing the same issues: I ended up using "Relationships" for the CCK related node field...
Would love to see an explanation of when to use Arguments with PHP validation vs PHP default argument vs relationships...

Thanks

andrewsuth’s picture

@letharion: I have the same issue. It's weird because it seems that the order of the arguments actually makes a difference. I've read nothing about this and have no idea why..

Try this:
Argument 1:
- PHP Code: return arg(0);

Argument 2:
- PHP Code: return arg(1);

It should return return "3" and "1" for your URL with "3/1"

Here's the weird thing, swap the order of the two arguments and it won't work..

esmerel’s picture

Status: Needs work » Closed (fixed)

Given the age of this issue, I have hope that it's resolved or no longer relevant.