Unraveling the logic of Views Argument Handling Code in Drupal
Views is one of the most important contributed modules in Drupal (right along side CCK and Image Cache) and though it is fairly easy to learn the basics of views, there are a number of advanced features that take some time to get your head around. One of these more advanced subjects is Views Argument Handling Code.
This tutorial is going to be a little too advanced for those new to drupal, but for those that are ready to get a better theoretical grasp on drupal's inner workings it may provide some useful insights.
Arguments and the url
Arguments are are strings (lines of text) in the url that are used by drupal to build a page. For example in the url translationdesigns.com/article-list/drupal/views the arguments would start after the .com. Argument one would be "article-list", argument 1 would be "drupal", and argument 2 would be "views".
These arguments are brought into Drupal as an array, and like most arrays the first index of the array is zero. This array, which arrives at the views handling code as the variable $args is what determines what information is handed to the view.
Note: $args should not be confused with (arg(1)). Though they deal with the same information the $args array is used in different situations.
The first element of the $args array, $args[0] is the url for the view and is not usually handled by argument handling code. Therefore the first actual element that is available for views to utilize is $args[1] which in the case of the url translationdesigns.com/article-list/drupal/views would be "drupal".
Now when you add arguments through the Drupal interface such as Book: Parent Node ID, Taxonomy term ID, Node Title, etc... those arguments can only be added in a linear fashion starting from argument 1. While this is fine for most situations, there are definitely times when such a linear arrangement can cause problems. One example would be when trying to make use of an argument that appears farther down url than argument 1. Drupal by default will always assign your argument handlers to argument 1, so how do you get it to ignore the first argument and use the second or third one instead?
Well here it helps to know a little bit about php array handling functions (or at least know how to Google to find what you are looking for.) PHP array functions give you the ability to do any number of things to arrays such as removing elements to the end, adding elements to the beginning and much more. In our case where we want to ignore the first argument we could use the array shift function to alter the $args variable.
array_shift($args);
return $args;
This does the trick so now what was $args[2] is now $args[1] and the old $args[1] has been taken out of use.
Once you understand that Views is expecting an array and that the argument handling code is just a place for altering that array before processing the only limit to what you can come up with is your understanding of php. If you don't get it right away cut yourself some slack. Drupal development is known to have a steep learning curve and with good reason.



kerunt
Helpful trick, thanks!
Enviar un comentario nuevo