I am using views and drupal 7 . I pass arguments in the URL and i want this as view page title. This is possible by using override title check-box. But i want to modify this title. i.e do some formatting and some trim of the content and show only what i require as title. how can i do it.
8 Answers
If I'm not mistaken, you cannot do that from the user interface. However, you can implement a hook in a custom module, take the available variables, apply your own logic and set the title you want.
You can find an example on Set page title for view in Drupal.
function HOOK_views_pre_render(&$view) {
if ($view->name == 'taxonomy_term' && $view->current_display == 'page') {
drupal_set_title('Your Page Title');
}
}
-
Hi can i get the argument passed in the URL in this hook function my_module_views_pre_render ?? so that i can modify and use it. Can i write this function in my template file?– HackerCommented Sep 27, 2011 at 11:18
-
The argument should be inside the $view variable passed to that function. Logic like this belongs in a custom module, not in the theme layer. Commented Sep 27, 2011 at 11:35
-
I also had to make sure the title of the View display was set to '<none>'. Otherwise the tile you enter on the view always overrides the value from the hook.– JohnCommented Jul 25, 2018 at 6:58
Found one that works
function MYMODULE_views_pre_render(&$view) {
if ($view->name == 'menu') {
$view->build_info['title'] = "TEST TITLE";
}
}
-
1This didn't work for me when I had render caching enabled Commented Nov 7, 2013 at 1:20
-
Caching was my problem too. Using hook_views_pre_view solved it for me (See the response from @MPD).– neuquenCommented Jan 16, 2015 at 20:25
I had a similar question in a slightly different context, When and where does Panels get the title out of a View?
My solution was
function foo_views_pre_view (&$view, &$display_id, &$args)
{
$view->display[$view->current_display]->display_options["title"] =
$view->display[$view->current_display]->handler->options["title"] = "Foo";
}
-
This hook worked for me, even with render caching enabled, but I had to also add drupal_set_title() for my use-case Commented Nov 7, 2013 at 1:51
-
In my case I had to change
$view->display['default']->handler->options['title']
rather than the$view->current_display
.– neuquenCommented Jan 16, 2015 at 20:20
You can use $view->set_title()
.
function mymodule_preprocess_views_view(&$variables) {
$view = $variables['view'];
if ($view->name == 'machine_name' && $view->current_display == 'display_machine_name') {
$view->set_title('New Display Title');
}
}
Instead of using a separate module, I added the following code to the theme I am using.
/**
* THEME_PREPROCESS_VIEWS_VIEW
* @param type $vars
*
*/
function MYTHEME_preprocess_views_view(&$vars) {
// view block
if ($vars['view']->name == 'viewName') {
//update your title
$vars['view']->build_info['title'] = '---Title---';
}
}
Replace MYTHEME with the machine name of the theme you are using. Add the code I shown in the template.php file of the theme.
You can implement hook_views_pre_render()
in a module.
/**
* Implements hook_views_pre_render
*/
function MYMODULE_views_pre_render(&$view) {
switch ($view->name) {
case 'VIEW_NAME':
// Your code here
break;
}
}
-
Thanks, but I dont see a "title" variable in $view with this function. Commented Jun 5, 2012 at 12:16
I have the same issue. The following is the only code that worked for me.
function MYMODULE_NAME_preprocess_views_view(&$vars) {
// If it is the view you want to change
if ($vars['view']->name == 'MYMODULE_NAME') {
// Override view's title, drupal_set_title() was not working
$vars['view']->build_info['title'] = 'MY CUSTOM TITLE';
}
}
I assume you have created a module "mymodule" and then used the hook_views_pre_build()
implementation.
I'm not sure you can change the View title at this step but there might be other possibilities.
First, if you are trying to change the page title (currently generated by the view page) you can try to add drupal_set_title('My new title');
.
function mymodule_views_pre_build(&$view) {
if ($view->name == 'view_machine_name') {
drupal_set_title('New title');
}
}
This does not really changes the View Title, but it can help to override the page title. If it does not work, it means Views output the page title after your hook and you'll have to find another hook.
Another solution could be to use the Views arguments as titles. Views has this feature built-in, there is a field for that in the arguments settings. You can check at the first argument in the default taxonomy view prodided by views, which provides a good example.
-
Much appreciated, no luck so far. I also have Page Title installed, and that somehow seem to override every other settings. Commented Jun 5, 2012 at 14:17