Sunday, March 1, 2009

Passing an argument to a Block View in Drupal 6

Passing an argument to a Block View in Drupal 6
Author: Kevin Godden
January 19, 2009

Q: How can I pass an argument to a ‘block view’ in Drupal 6?

A: There is no way to pass an argument to a block view in Drupal 6, but don’t panic as there is a way to achieve the same result through some slight-of-hand.

The use of arguments with Drupal views are vital for getting the most out of the views functionality. How are the arguments normal passed to a view? Well, if a view is configured to produce a page then the arguments are easily passed as part of the requesting URL, while if a view is embedded using code, then the arguments are passed in as part of the call to embed the view. But if a view is configured to produce a block, how do you pass arguments to it? The bad news is you can’t - the good news is that there is a way fake it and achieve the same result.

The trick involves providing a PHP handler within the view which will be called when the view is invoked without an expected argument (this is what happens when the block is displayed!). We just arrange for this handler to retrieve and return the argument’s value and then the view will behave as required - just as if the argument had been passed to it in the first place.

To do this create the block view as normal and configure the required argument(s). For each argument we choose the ‘Provide default argument’ option, and select the ‘PHP code’ sub-option. We then provide some PHP code which will ‘get’ and return the argument’s value, it doesn’t really matter where or how the PHP code gets the argument once it returns the correct value. Have a look at screen shot below:

Adding a default parameter

The example above is a bit simplistic as the PHP code just returns a static value - not very useful at all! A more realistic or useful example (inspired by one of the posts referenced below) would be to return the argument that was passed to the page that contains and displays the block. Consider the mythical paths:


Here things are set-up so that ‘web-design’ and ‘illustration’ are arguments to the ‘projects’ page, they result in only the projects of that type being displayed. Assuming we are using the pathauto module for nice clean URLs (as we almost always are!) then the following PHP code when provided as the default argument handler will get the URL, parse it and return the argument part to the view.

$path = drupal_get_path_alias($_GET[‘q’]); //get URL alias $path = explode(‘/’, $path); //break path into an array if ($path[0] == ‘projects’ && $path[1] != ”) { return $path[1]; }

So there is is, it’s definitely not the easiest method in the world but at least it does provide a mechanism of getting those arguments to the view…


Related Posts

* neat_trim() - Handy for tailored drupal teasers
* Technical Support Service for irish Web Designers Launched
* Drupal cron setup on unix plan

This entry was posted on Monday, January 19th, 2009 at 3:01 pm and is filed under Tech Stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Passing an argument to a Block View in Drupal 6”

1. Oliver Says:
February 10th, 2009 at 6:39 pm

Why not just do…?
if (arg(0) == ‘projects’ && arg(1) != ”) return arg(1);

2. Kevin Godden Says:
February 11th, 2009 at 9:36 am

Hi Oliver,

I was using drupal_get_path_alias() with explode() etc. because I have the pathauto module enabled and drupal_get_path_alias() returns the nice path with ‘projects’ etc. in it rather than the internal node id type path (if you know what I mean?!)

No comments: