I think I'm having a problem wrapping my head around the basics of how blocks take arguments in Views2.
I have two content types: Articles and Issues. Articles has a node reference field to Issues, which will allow me to list all the Articles associated with a given Issue.
I would like to output a block view that takes the shared field as an argument, and output a simple list of article titles. This works fine when I add the block to an issue node: I set the default argument to be "Node ID from URL" and it works. If I try:
$arg[0] = $node->nid;
...it doesn't work, however.
Since the actual node ID isn't really in the URL (the URL is assigned with pathauto and tokens), I'm assuming this option really just pulls the node's id from somewhere else, correct?
The problem comes when I want to list this block on each ARTICLE page. What should my argument handling code be? I know the correct value for the argument is stored in $node->field_issue_title[0]['nid']. I tried setting my argument handling to be:
$arg[0] = $node->field_issue_title[0]['nid'];
...but that didn't work. When I tried echoing that value in my content template it worked ok, but echoing it in the block header did not work.
Is it even possible? I know the correct value is available to me, but how to send it to the block?
Comments
No Node Loaded
Since the blocks can be placed on any particular node, by default they do not load any of the data of the node they are residing on. You have to manually add the node's properties using the node_load function: http://api.drupal.org/api/function/node_load/6
Even if you're using pathauto, the actual URL is node/x, where x is the node's ID. So you can use
$node_id = arg(1)
to pull in the relevant node's ID and then grab that field (
$node->field_issue_title[0]['nid'];
)i think i understand the
i think i understand the difference with blocks now, much thanks.
i still can't get the argument set up properly, however.
the block's argument is set to be my node-reference. the article nodes reference the issue nodes. if i want to output a list of articles (on the articles node) that depends on a referenced issue, what would my exact argument handler look like?
if i wanted to place the block in the article node:
would pull the article id, not the issue id. i guess i'm just confused as to how to bring these two together.
here is the argument i have
here is the argument i have that i thought would work, but does not:
Try this
Try this instead:
that worked like a charm.
that worked like a charm. thank you so much!
Bookmarking, greetings,
Bookmarking, greetings, Martijn
Thank you very much
Hi,
Your solution is really very good and neat one. It helped me a lot.
God Bless you.
Good for single field, but what with multiple fields?
Hi,
thanks for the code, it worked for me in my D6 view.
But what can I do to display multiple fields, acually multiple values from one field?
Now it only displays the first value "return $node->field_issue_title[0]['nid'];", of course if I change it to "return $node->field_issue_title[1]['nid'];" it will show the second value.
But what is the trick to display all the values?
Was Receiving Page not found 404 (Drupal 6)
I know this is an old thread and that there's lots of places out there that have example code for passing arguments to block views, but I couldn't find anywhere else that addressed specifically what was happening to me. Hopefully it will help someone else with the same problem, and possibly someone can look at my solution and see a better way.
I used this same method, and it worked for me eventually, but not until I added url aliases for all of my arguments. That is, I had a URL like this:
www.example.com/states/Idaho
where the block view is configured to show on the "states" page as well as any pages that have "states" at the beginning of their path. I got my view working so that passing it the argument "Idaho" in the Views preview area returned the result set I was looking for, but when I tried using the URL above (the actual "states" page with the "Idaho" argument in the URL), it gave me a 404 error.
I had to add a URL alias of "map/Idaho" to my "states" node so it would bring up the node at all. Once I did this it could find the node and subsequently load the block with the right argument passed to it.
This works but seems like a bit of a kludge. Does anyone else know of a better way?
In your page.tpl.php use
In your page.tpl.php use
print views_embed_view('[Your View Name]','default', ARGUMENT-YOU-WANT-TO-PASS);
In case you wish to pass nid your argument would be arg(1).
Your view should be expecting the same argument.