Limiting Content Using Arguments And PHP Code
Note: I have asked that information like this be added to Advanced Help within Views. See http://drupal.org/node/495634
This article assumes you have a basic grasp of Drupal, Views 2 and PHP
In a recent project I needed to limit nodes returned by Views by a session variable, and found it hard to figure out how.
I created a View to show all nodes of a certain content type, and then used an argument on a CCK field to only show associated nodes.
First, using the "Action to take if argument is not present: " section, I selected "Provide default argument".
Then, I changed the "Default argument type: " to "PHP Code".
Here you can add arbitrary code to the box which will be executed like a function.
return $_SESSION['MySessionVar'];This way, you can limit certain nodes by content, such as CCK variables.

Show filtered users by common variable
After two years of trying in spare time I still feel I do not have a basic grasp of Drupal, Views 2 and PHP, but still I'm trying (on my own) and, as can be expected, get stuck frequently, maybe on even supposedly simple/stupid things.
I am trying to develop a simple student-club (historic members) site. Each college year has some 15 members in the club of that year and then there is the overall (vertical) relationship.
For the moment I and have just managed to produce the listing of the members of my year (1983) through views. On signing up the members are required to indicate their year by a profile field, which for the moment is limited to 1983, but it's meant to hold several years.
In views I intend to filter the member list for each year based on that field (club8year). However, Views only allows me to select a specific year, not a variable year corresponding to the current user's club8year, which is what I need.
Views' query is as follows:
WHERE (users.status <> 0) AND (users.uid not in ('0')) AND (profile_values_profile_club8year.value in ('1983'))
TWO PROBLEMS:
1 - I'm not even able to FIND the php file where this query lives (no references found on drupal.org after too long searching)
2 - HOW can I change the code so that ('1983') is not a fixed year, but the same year as the users' club8year in the profile?
I understand it must be something like:
WHERE (profile_values_profile_club8year.value in ('current.user.profile_values_profile_club8year.value')
'))
Thank you for helping out!
Similar Problem
I spent a few hours beating my head into my desk over a similar problem. Hopefully this helps someone out a little bit.
We were trying to build microsites with a view block left nav propogated by a CCK field. It's no trouble to use a static argument for this, but we needed a dynamic year value, much like what you're trying to do here, but with nodes rather than user profiles. The year value that we were trying to grab was a CCK select menu for that node type, editable by content admins.
This is the logic that I used in the argument code
<?php
$p = explode('/', $_GET['q']); //we need the node id, so we need the query string without the path alias
if(is_numeric($p[1])) { //this should be the node id
$n = node_load($p[1]); //load the node into scope
return $n->field_whatever[0]['value'] ? $n->field_whatever[0]['value'] : FALSE; // if no node is loaded or this value hasn't been set, then return false.
}
?>
Haven't worked with profiles yet, so I don't know if this type of logic will help.
Use arg(x)
Use the arg function rather than exploding the $_GET variable.
BTW: start with arg(0)