Views 2.0 – Argument transformieren mit dem PHP Validator


Ich habe den folgenden Teil als Hilfedatei für die neue Views 2.0 geschrieben. Sorry, ist in Englisch… die Drupal Welt spricht nun halt mal einfach Englisch. Die Codebeispiele sollten jedoch auch für deutschsprachige gut verständlich sein.

Using Arguments

Using arguments is not as simple as it might look at first glance. It's not as complicated either. Using arguments applying a transformation might be a little tricky though.

What's it all about?

The argument allows you to pass options (arguments is actually the better word) at runtime to the views. So lets say you want to build a views that displays different things depending on where your are. You won't be able to use filters, because they are static. That's why we use arguments.

If you are in clean URL Mode you often have URL that might look like that: www.mydomain.com/node/23. Argument 1 (or arg 1) would be 23. That's where this option of views comes in. Technically said, the argument is a where condition in the SQL statement. In the next few sections we are going through the different kinds of arguments.

Choose the type of argument

There's a whole like of arguments we can choose from. It's pretty simple to explain it, looking at the SQL. This would actually be the part …. WHERE myArgument = value. So first we have to choose the first part of the WHERE clause.

Default options

There's a bunch of default option like Title, what to do if there's not argument and so on. They are straight forward. Some testing will quickly reveal the function.

Validator – PHP Code

This is the part where the Arguments become incredibly flexibel. We can transform an input argument. Main reason is though to validate the argument. This means if we use the PHP validator code we have to ensure that we return a boolean value. Here a little example code:

The variable $argument holds the argument from the URL. If you try to use arg() it won't work! To store the new argument again we use the $handler->argument variable. If we had choose as argument type the node id, and the initial input argument value would be 23 the SQL statement would look something like that: … WHERE nid = 46 (this might not make any sense, but it shows the point).

We can then further specify what action we are going to take if in the PHP validator we return false.