Passing an argument to a Block View in Drupal 6

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:

and

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.

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

23 replies
  1. Kevin Godden
    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?!)

  2. Lizbeth
    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…

  3. Theresa
    Theresa says:

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

  4. Tootsie
    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?

  5. schefz
    schefz says:

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

  6. chiMaxx
    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.

  7. Johnathan
    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?

  8. Chetan
    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.

  9. Chris
    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

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

  11. Sathesh
    Sathesh says:

    Hey there.. Thanks for putting this up. It really saved me some time in setting up the view arguments with blocks. Thanks dude..

  12. Oliver
    Oliver says:

    This is great. Been trying for a while to make a block appear on user pages with a list of content they’ve authored. This is a good solution. Thanks!

  13. William
    William says:

    Will this work in drupal 7? Can anyone help me out here? I can’t seem to get $_GET to work. I’ve tried returning a constant and it worked, I tried storing an integer in a variable and returning the variable, and it worked, but $_GET just doesn’t seem to get the argument value.

    I even did the most basic thing I could think of and it just won’t work.

    I typed http://localhost:8082/?q=12 into the address bar.
    I have my php code as

    $path = $_GET[‘q’];
    return $path;

    The block is outputting null instead of content 12.

    I tried everything. Suggestions?

  14. dan
    dan says:

    Thanks for this article. It got me out of a bad situation. If you are having problems making this work, it could be that you are using punctuation in your title and have pathauto set to remove the punctuation. It seems that this way of doing things pulls the argument from the node title before pathauto removes punctuation.

Trackbacks & Pingbacks

Comments are closed.