Drupal: Validate Views Arguments

As previously mentioned, this site is now powered by the powerful open-source content management system (CMS) known as Drupal. While things are working pretty well, (if you ignore the fact that the number of visitors has dropped by 2/3rds; that’s a separate topic) there was one minor issue that was been bugging me, and I got it sorted today.

I use the Views module to generate the index listings for the annual and monthly archives. The setup for that is pretty straight-forward and rather than describe exactly what I did, I’ll just point to Rob at Late Night PC, whose approach is very similar to mine. (At the bottom of this article I’ve posted a screen shot of my view design page in case it’s helpful to anyone.)

The problem was that when an invalid URL was entered, Drupal didn’t generate a 404 not found error, it simply did its best to display something. Until and unless Rob gets a chance to fix his site, you can see the problem demonstrated by clicking on this link to a non-existent page on his site.

More specifically, if one tried to visit /archives/garbage/, then one would have seen a bogus archive page, not a 404 page. Bogus in the sense that no content was displayed, but the page title reflected that this was the “garbage archives.”

Similar results were obtained for /archives/2005/garbage as well as for /archives/2005/10/garbage.

The reason for this improper behaviour was that the view I created did not validate the input arguments. So where years should only consist of 2002-2009, any value was accepted and processed. Same for the months, only 01-12 should have worked.

The actual article name argument couldn’t be specified so easily, but as we’ll see, there was a fix for that as well.

To validate the year, I specified the validation mode as “PHP” and used the following code (I’m sure a PHP maven could produce better code, but it does work):

switch ($argument) {
  case "2002":
  case "2003":
  case "2004":
  case "2005":
  case "2006":
  case "2007":
  case "2008":
  case "2009":
  case "2010":
   return TRUE;
return FALSE;

Similarly, I used the following code to validate the month argument in my view:

switch ($argument) {
  case "01":
  case "02":
  case "03":
  case "04":
  case "05":
  case "06":
  case "07":
  case "08":
  case "09":
  case "10":
  case "11":
  case "12":
   return TRUE;
return FALSE;

So far, so good. But what to do about the actual article names? Obviously, there’s no pre-set pattern that could be used.

What I did was add a third argument, of type Global:null, which served as a placeholder for the 3rd argument, the page title. I then validated this argument using the Node validation selection, and specified the validation criteria as “user has access to node.” This works because if the node doesn’t exist, then no one has access! (Thanks to Freeman at broken at the next release for the idea of using Global:null as a placeholder!)

So there you have it! Annual and monthly archives that are similar to those in a default WordPress site, but which also generate a 404 not found error if the page name is entered incorrectly.

Here is a screen shot of my view design page:

drupal view