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:

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


Related Posts


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


  1. Oliver Says:

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


  2. Kevin Godden Says:

    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?!)


  3. Lizbeth Says:

    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…


  4. Theresa Says:

    Hey, thanks for posting this! It was the quick fix I needed for a problem that was plaguing me.


  5. Tootsie Says:

    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?


  6. schefz Says:

    I am in the same boat as Tootse. followed the post, but nothing shows. Has something changed with views?


  7. schefz Says:

    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!!!


  8. chiMaxx Says:

    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.


  9. Smath Says:

    Just wanted to join in the list of people saying thanks for this :) You saved me a death-match with the panels-monster!


  10. Mein Wissensblog » Drupal / Views: Argumente an einen Block übergeben Says:

    [...] Alle Infos findet ihr hier: Link [...]


  11. zaphod Says:

    Greaaaaatttt!


  12. Johnathan Says:

    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?


  13. Chetan Says:

    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.


  14. Luigi Says:

    Thanks alot Kevin.


  15. Chris Says:

    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! :D


  16. Chris Says:

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


  17. Batido de naranja - by victorcoder » Pasar parametros a bloques de vista en Drupal Says:

    [...] Referencias: http://www.ridgesolutions.ie/index.php/2009/01/19/passing-an-argument-to-a-block-view-in-drupal-6/ [...]


  18. Corrie Potter Says:

    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!



Leave a Reply