By AndrewJarvis on
I want to add some logic into my view so that it will pull a variable (such as the node title or a cck field) of the node that the view is being displayed ON and then filter the view results based on that. So that if the view is displayed as a block ON the page "popcorn" the view will only only return results that are "popcorn" (like, lets say for example popcorn was the value in a cck field, or a taxonomy)
Thanks!
Comments
...
Filtering is done using arguments. Since you can use PHP to set the arguments, you can make them whatever you like.
First a simple example where the food is hardcoded:
And now lets pull the food from the node displayed:
(Instead of putting this code in the 'Argument Handling Code' you can also pass an argument array directly to views_build_view().)
Thank you so much!
Awesome! I still have some questions...
1) Okay, I want the title of the page that the block is being displayed ON to match a field in the results nodes, so if I put in the argument handling "$node = node_load(arg(1));" - that will load the variables for whatever node the view block is being displayed ON, and not just whatever node result it happens to be looking at at that moment, right?
2) If that is correct, then the followup question would be, how do I get information about those individual nodes its examining to determine if they fit the criteria?
3) Do I have to do this blind? Usually I make ample use of print_r($variable) to see what I'm doing - apparently printing things in this field doesn't show up anywhere on the page.
4) Lastly - everything I'm trying is in the "argument handling", but what about the argument itself - I honestly have no idea what those do and the views documentation tells me precious little about the fields:
Argument Type | Default | Title | Option | Wildcard | Wildcard Sub | Ops
Thanks again!
...
(Sorry for not replying earlier.)
First, a little bit more about arguments:
Perhaps the main feature of Views is that it lets you filter the results. That's what 'Filters' do. 'Arguments' are just like filters except they are entered in the URL. In other words, whereas 'Filters' are hardcoded in the view definition, 'Arguments' are dynamic.
(Actually, 'Filters' too can be made dynamic, e.g. by 'exposing' them.)
For example, you can setup a view to show all the nodes of a specific type. You do this by adding the 'Node: Type' argument to your view. To get all 'story' posts you'd call the the view thus:
example.com/?q=posts-by-type/story
(assuming 'posts-by-type' is the URL of the view.)
The 'Argument handling code' lets you tweak the aguments before Views filters by them. For example, if we use there the following code:
Then in the year 2009 we will see only 'poll' nodes, no matter what the URL contains (of course, this is a silly example.)
====
Yes.
$node = node_load(956)loads the node having the ID 956. But of course we can't hardcode the NID. We have to figure out what it is. The way we do this is by examining the URL. In our case the URL is of the form 'node/xyz', where 'xyz' is a number (this is true even if we use some nice alias for the node). We need to extract the 'xyz' component of the URL. Drupal provides us with thearg()function, which extracts components from the URL. To extract the second componen we'd doarg(1)(in programing languages it's common to start counting from zero).(Of course, URLs aren't always of the form 'node/xyz'. But I assume you put your view block on such pages.)
What fits the citeria?
The node shown? It does't fit any criteria.
When you navigate to 'q=node/123' you see node #123 without regard to some criteria.
I don't have access to Drupal right now, but if a simple print_r() doesn't work for you, try:
An alternative to 'print_r()' statements is to use the 'devel' module, which gives us a tab to see all properties of a node.
Since 'arguments' are provided in the URL, they can either be there or not be there. Most of the settings you listed tell Views what to do in case the argument is missing, or... present.
The most used setting is 'Default', which tells Views what to do when the argument is missing.
(I did some simplifications in my reply. Naturally, there are more details and features to discuss.)
WOW! THIS IS FANTASTIC!
This gives me a lot to go on, and I've been playing with it - thank you so much!
The one snag I've hit now is that it simply will NOT "node_load" within this argument handling area - it just seems to not do it...
I tried this:
And it just returns "1" (which is what it returns if the variable is totally empty)
Here is also an extreemly verbose version designed to troubleshoot what exactly the problem is - and all the variabels do what they're suposed to, except for the node_load which still returns "1"
So, I guess if I cannot node_load, is there a way to just pick out the node title, since that's the specific variable I'm after?
...
I believe the problem isn't in node_load() but in how you print the node. You forgot one parameter to print_r(). It should be:
and not:
This '1' parameter tells print_r() to return the string instead of outputing it.
===
The only possibility I see for node_load() to fail is when you feed it a wrong NID. That is, when
arg(1)doesn't return a valid NID. So instead of trying to print out $node, first print outarg(1). Remmeber thatarg(1)simply pulls the second component of the URL, so you should be on a 'node/xyz' page for the code to operate correctly.(UPDATE: I see from your second snippet that you do have a valid NID, 39, so the problem is most probably the omission of the second parameter to print_r().)
YOU ARE MY FAVORITE PERSON!
You rock dude, THANK YOU! Here it was Node_Loading the WHOLE TIME! hahaha... God, i really feel like I UNDERSTAND views so much better now!
I think that TOTALLY solves my problem now!
Until the next snag I hit,
Andrew
...
Oh, this question has little to do with our discussion :-)
But I first need to understand your rationale.
Thank you for an useful discussion!
Mooffie, thank you for all this information. I'm trying to wrap my head around arguments and this was useful. What I'm trying to do is simply display the title of the node currently being viewed within a block. So, I created a view for a block, list view of node titles. I used the following for my argument code
and I got a big ugly red error. Can someone assist me?
...
You don't need to use Views for this (it'd be an overkill).
I'll answer there.
(Welcome back! BTW, Views2 nowadays can easily do your garage sale, no need for 'Node Fusion' anymore. It can even group by street.)
Do I need to use arguments for my view?
Let's say I have a CCK type called Pizza.
Within that type I have a checkbox list of Toppings.
I create a new pizza called Mighty Meaty and select 8 toppings.
When I go to example.com/pizza/mighty-meaty I want a block that lists those 8 toppings.
In my view -
Fields -
field_toppings
Filters -
Node Type is one of Pizza
I guess I need to add in an argument somewhere to tell it to only return toppings for the current pizza being looked at but I can't figure out what exactly to do.
...
You can use the 'Node: ID' argument to limit the view to one specific node. In the 'argument handling code' do '$args[0] = arg(1);'.
Thanks
I figured that out right after I posted and went back through some more samples.
Then I added visibility code to only show for node type pizza and it works great.