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

Zyxware default image4

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.