Change a title of a view block programmatically!

10 Nov 2011

Posted by CrashTest_

Just in case you are like me and find yourself wanting to change the title of a view programmatically, and can't figure out how, the (somewhat unintuitive) way is to hit the build_info, so you do this in your module:

<?php
function hook_views_pre_render(&$view) {
 
// Some code to make sure you are changing the right title, say an if statement perhaps!
 
$view->build_info['title'] = t('Your Brand New Title!');
}
?>

Soon this will not be necessary, as there will be a much easier method for accomplishing this included in Views sometime in the near future.

<?php
function hook_views_pre_render(&$view) {
 
// Some code to make sure you are changing the right title, say an if statement perhaps!
 
$view->set_title('Fancy New Title');
}
?>

Hope this helps!

Sitewide Terms: