My working theory on methods for defining Views Arguments.
a quick note on drupalabels : what the View-edit page calls 'Arguments' I will refer to as 'Argument Handlers' because they do stuff with the Arguments that are passed to them; the Handler is a 'function' and the Arguments are the '$variables'. I will therefore use 'Arguments' to refer to the data in the 'args' array.
Getting the right Argument to the View's Argument Handler can be tricky.
As far as I know, there are only two 'carriers' from which a View Argument Handler can get the Argument:
- 1: URL (passive method)
- As far as I can tell, Views' default (off the peg) Argument Handlers try to pick out a particular section (NID, UID, TID, etc) from the context-node's URL (eg
www.site.com/node/23... so the NID = '23') and use this as the (first) Argument ($args[0]). - Every page has a URL, so there's always something for the Handler to work with. But because URLs can get messed about so much by modules and such, this method often seems (IMHO) to yield unpredictable results. Of course if you can predict exactly how your URLs are going to be structured, you only need to customise is the View's Argument Handling Code to translate the URL structure into the View's $args array structure.
- 2: views_build_view() (active method)
- The context-node (or module or whatever) itself specifies the Arguments (the args array) and sends it to the View when calling it, using views_build_view() as the carrier.
- The downside is that you have to figure out where and how to insert (hack) the following php code (or similar) into the context-node, module, node.tpl.php, block, or whatever it is that calls the View into being. For example, the NID is a property of the context-node's $node_array (eg $node->nid), not the view, so we need to define the Argument within something that is part of or has access to that context-node's $node array. IMHO this method is more reliable and predictable ... in a sledgehammer sort of a way ;)
- Calling the views_build_view function
<?php
global $current_view;
$current_view->args[0]=$node->nid;
$view1 = views_get_view('view_name'); // change 'view_name' to the name of your view
print (views_build_view('embed', $view1, $current_view->args, false, false)); ?>
This page is a work in progress. I will add to and edit as I get a better grasp of what's going on and why, but I hope in the meantime it may help other drupaleers struggling up the north face of Views.
Of course if anyone has any advice, suggestions, theories, explanations or improvements; or especially if I've misunderstood something - please, please, please do jot them in a comment or contact me (JohnG) and I'll gladly incorporate them as best I can.
No comments:
Post a Comment