views:

30

answers:

1

Hi there,

I currently having trouble to get my head around how Drupal's views work with arguments when displaying a view as a block. I have a few simple pages (eg studios/bremen) and a few users that match those pages (eg username: bremen). Users can post news which need to be teasered on the studios/* page.

Do I have to use an Argument Handler that translates the node_id in arg(1) to the corresponding username, or is there an easier way?

I hope i could explain my problem so you can understand… this whole view argument thing feels kinda weird to me.

+1  A: 

arg(1) won't be a username or UID and not work the way you expect. What we can try though is to do a lookup and return to provide the default argument value in a roundabout way.

ACTION TO TAKE IF ARGUMENT IS NOT PRESENT: set to Empty text, or, 404, whatever your preference.

Provide PHP code for the Validator:

if ($argument) {
   $user = db_fetch_object(db_query('SELECT name FROM {users} WHERE name = "%s"', $argument));
   if ($user) {
      $handler->argument = $user->name;
      return $user->name;
   }
}

The part you want to make sure about here is that arg(1) being a username on certain pages will not contain (or allow) characters to pass through causing a SQL injection.

Username patterns can be controlled with Pathauto. This won't stop someone from entering junk into the URL though, and Drupal/Views should be able to sidestep characters in the URL. You can test that by setting ACTION TO TAKE IF ARGUMENT DOES NOT VALIDATE to display a 404 page, or, empty text.

Try this out and use the preview part of Views and see if it returns anything for you. I did this off the top of my head, but I do similar things all the time with different types of content or searches.

You can also try setting the Validator to User, and Allow both numeric UIDs and string usernames. I haven't used that setting, but it most likely will work as well.

Kevin
This doesn't work - or i am doing something wrong. What do i set the argument to? "User: Name" oder "Node: ID"?. What's the best way to debug the code anyway? Any output seems to be ignored.
Nils Riedemann
Try User: name. Your argument is providing what to select based on your Filters (node type, node status), with the base table of Node (hopefully).
Kevin

related questions