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.
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:

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:
www.somedomain.com/content/projects/web-design
and
www.somedomain.com/content/projects/illustration
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…
Sources: http://drupalsn.com/learn-drupal/drupal-questions/question-2650, http://drupal.org/node/332521

February 10th, 2009 at 6:39 pm
Why not just do…?
if (arg(0) == ‘projects’ && arg(1) != ”) return arg(1);
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?!)
March 6th, 2009 at 11:41 pm
Hey thank’s for the article but how can I use it if I want to detect the taxonomy term that a node has??? It is shown in the url and I want that all of the nodes that share that taxonomy term can have that specific block view…
April 27th, 2009 at 2:10 am
Hey, thanks for posting this! It was the quick fix I needed for a problem that was plaguing me.
May 15th, 2009 at 2:38 am
I found your article to be very clear, but when I tried implementing it, I was unsuccessful. My path (via pathauto) is mysite.com/communities/area1. I want the argument to be area1. So I have the exact code as above, but I changes ‘projects’ to ‘communities’. Nothing comes up in my block. Any ideas?
May 22nd, 2009 at 4:55 am
I am in the same boat as Tootse. followed the post, but nothing shows. Has something changed with views?
May 22nd, 2009 at 4:16 pm
hey tootsie,
i think its a copy and paste error from the code in this post. I used the code from this post here:: http://drupalsn.com/learn-drupal/drupal-questions/question-2650 which is the exact same thing, and it works!!!
August 11th, 2009 at 6:27 pm
This blog apparently has typogrify on, so the quotes in the code are incorrect when you paste it. Change the curly apostrophes to straight single quotes, and change the curly close quote mark at != ” into to straight single quotes !=” and then the code should work.
August 11th, 2009 at 8:43 pm
Just wanted to join in the list of people saying thanks for this
You saved me a death-match with the panels-monster!
September 2nd, 2009 at 9:50 am
[...] Alle Infos findet ihr hier: Link [...]
September 10th, 2009 at 3:58 pm
Greaaaaatttt!
October 23rd, 2009 at 7:54 pm
I can get the block to accept the arguments, but whenever I try to use mysite.com/projects/2009 (where 2009 is the argument), I just get a 404 page.
Hasn’t anybody else hit this problem?
Right now I just have the block display on every page, so even on the 404 page I see that it’s accepting the arguments.
How should I implement this method without getting 404 pages?
October 31st, 2009 at 4:14 pm
Hey, Great man, you helped me out!!!
Just wanted to add some notes.
If pathauto is not there try simple one :
$path = explode(’/', $_GET['q']);
return $path[1];
and also for the folks : when you copy & paste please look for the quotes, they might be rendered in differant way for i18n.
Rest is fine work! I checked it on Drupal 6 & basic view.
November 23rd, 2009 at 3:44 am
Thanks alot Kevin.
December 1st, 2009 at 6:14 pm
I’m almost ready to post an ‘AWESOME!’ comment, but am not quite there just yet. I have a node located at exhibitors/exhibit-list that has some pdf attachments, copy, etc. (so I can’t create a page view here as non-webby people maintain the content) I also have a block view assigned to the same page/path that shows a list of exhibitors, all of them to start. Now, if I enter a URL like:
exhibitors/exhibit-list/b
I get a page not found, because I’m guessing the path is different now. With some minor adjustments to the code I have everything working fine in the views preview. Arguments are being understood there thanks to your help!
Any thoughts on what I might be missing? Do I need to set up something special in pathauto? Thanks for your advise in advance!
December 1st, 2009 at 6:44 pm
I made a temporary solution that works for what I need but hurts a little. After incorporating your suggestions, I set the block view to show at:
exhibitors/exhibit-list
exhibitors/exhibit-list/*
Under URL aliases, I created 26 aliases (which may grow if we get exhibitors that start with numbers), like this:
node/XX
exhibitors/exhibit-list/c
That works for now, but if there’s a better solution, let me know.
January 11th, 2010 at 6:47 pm
[...] Referencias: http://www.ridgesolutions.ie/index.php/2009/01/19/passing-an-argument-to-a-block-view-in-drupal-6/ [...]
March 4th, 2010 at 8:27 pm
Great post. I have been banging my head on the ground trying to figure this out and it was something completely simple and was a simple miss read of the directions. Make sure that you DO NOT have brackets surrounding your code. It will cause it to not work! Thanks again!