Last updated June 18, 2009. Created by Robin Duckett on February 10, 2009.
Edited by jhodgdon. Log in to edit this page.
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.
Comments
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!
I started with Drupal in 2007 and then my life got stuck...
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)
Other way
<?phpif (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
return $node->field_somefield[0]['nid'];
} else {
return FALSE;
}
?>
And other tips at hankpalan.com
Somewhat a similar issue... Views/Node Repeat
Hi, I hope you can help me. My issue is, I have a set of data being pulled out of CCK fields. I am trying to have certain fields display on related fields. I managed to achieve this by Arguments--> Node: Nid--> Provide default argument--> Node ID from URL. However, the problem rose when I split a node using the Node_Repeat module. The reason for splitting the is because it's for shows on a calendar and the ONLY difference between them are the start times. Anyway, this gave the same "content" different node IDs. This means that the times in the right content ONLY relates to the specific nodes, where as I need to have right side content for all the 3 related nodes to display in the one place. Please help if you can.
Harry
Very confusing. All other
Very confusing.
All other examples I find tha i need use [nid] instead of ['nid']
anyway, no difference, it does not work for me
<?phpif (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
return $node->field_land[0][nid];
}
?>
Does not show other nodes with the same country code. Is there perhaps any way to check what the code actually returns?
Arguments based on fields in the node displayed
I've created 2 custom types applications and categories. I've got numerous views showing the applications sorted by a particular field, but I want to filter them by category. Sounds easy I thought.
So I've got a category node page, where the url is the alias of the category (so /category/demo for the demo category for example).
What I want to do is lift the categoryid from the category node some how, and display, beneath the category node, a view containing some of the applications within that category.
From what I can see the only way I can do it is to add code to the node-application.tpl.php page, which is nasty, I was hoping I could somehow use an argument to generate a view, which would somehow get the categoryid from the page and filter it's results. I thought I could do that with a block, but apparently not.
Can anyone give me some suggestions?
I solved this by creating a function, which used the URL to identify the current node id, then looked that up in the database to get the categoryid. So I used default arguments, with php code, to call a function, which returned the categoryid, so as to filter by that field.
Hope that helps anyone trying the same thing as me in the future
Without any coding
I don't understand why you created a new content type for 'categories'? Sounds like it could be a taxonomy term or an additional field and you could add to your numerous views using this taxonomy term or new category field.
However, I'm assuming you want to relate multiple instances of one content type to a single instance of another content type; i.e. a single parent and multiple children. So, your parent content type instance of 'category' would be 'demo', and your child content type instances would be 'application'.
To do this without coding, you need to add a node reference field to the 'application' content type that points back to the 'demo' instance of a 'category' content type. Now, you can add a block view that uses the node id of the 'demo' content to list all of the 'application' content records. This (view) block needs to be added to a region for display. Your block view should use an argument and relationship, and you need to provide a default argument using the node id. This is more complicated to explain then it is to set up, so just play around with the arguments area of the edit views display.
I my use case I had a billing statement content type with multiple line item content type. When I display a specific bill, I see all of the related line item content. I'm displaying the line_item block view in the content region so all of the line items show up after the billing statement content.
One major problem with this
One major problem with this solution seems to be that the view can't be cached as it is "customised on the fly", cf. http://drupal.org/node/353428
"I now have to find a way to use cache and only produce the pages individually when there is a need according to my session values".
EDIT: another great thread about this solution, with a detailed exemple: http://drupal.org/node/433672
Interesting resources for developers? Have a look at appsta!