Change the page title of a view in Drupal + Views 2 using Argument Handling Code

September 10, 2009 - 07:05

If you create anything other than a very small site using Drupal you will end up using Views and CCK, two of the most important contributed modules in Drupal. Views allow users to dynamically create different presentation of tabular data from the database. However this also necessitates dynamic page titles for pages using views.

In Views 2 you can change the title of a page generated using views programmatically by the use of argument handling code. In Views 2 you can enter argument handling code by selecting 'PHP Code' as the validator type and then entering PHP code in the text area shown. Remember to not use <?php and ?> when you enter PHP code there.

Figuring out the logic to create the title should be easy for any PHP programmer. But figuring out how to set the title might not be so. You can set the title of the view as follows.

$view->display['display-identifier']->handler->handlers['argument']['argument-identifier']->options['title'] = 'Your custom page title';

In the above snippet display-identifier is the identifier for the display for which you are setting the title. You can figure that out by hovering your mouse over the displays and taking the id from the URI. Similarly the argument-identifier can be found out by hovering your mouse over the argument in the Arguments section.

$view->display['page_1']->handler->handlers['argument']['type']->options['title'] = 'Your custom page title';

In the above example page_1 and type are the ids.

Get Drupal updates straight to your inbox

To prevent automated spam submissions leave this field empty.

Post your comments / questions

This is very useful one when overriding nodes created by other modules using views.

Hi, thanks for the tips. That was a great help to me...

I can't work this out!

I get the code, I've dropped it into my views-view-table--Bender.tpl.php - not much action here

Looking at other obvious places to put the code.. 'Title' for the view I've created, consisting of only 1 page... Nope, this just spits the php into the title :-)

I'm using;
$view->display['page_1']->handler->handlers['argument']['nid']->options['title'] = 'Your custom page title';
-- 'nid' because my View takes in a single parameter - nid, and spits out the contents.

I'm sure it's obvious, but to me, seemingly not.

Thanks in advance,


Click on one of the arguments in the Views admin UI. This will pull up the argument settings form below. In the validator drop down select PHP code. Enter the above code in the textarea and then ensure that you return TRUE if you do not have any other validations to be done. If you already have a standard validation on your existing argument you should be able to use the global argument as argument 2 and then use the argument handling code for this second argument.

If you want professional help let us know and we can gladly take care of your problem.

Anoop John
Team Zyxware

In the title field of the Argument you should be able to just drop in something like "Gallery Category: %1"

Guess this is a way to take it farther.