8

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.

2
  • There is now Page Title contrib module that offers Views integration as well.
    – LLub
    Commented May 8, 2012 at 5:19
  • You can achieve it with the Metatag module. This module allows you to automatically provide structured metadata.
    – heilop
    Commented Jan 16, 2013 at 15:30

8 Answers 8

7

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');
  }
}
3
  • 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?
    – Hacker
    Commented 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.
    – John
    Commented Jul 25, 2018 at 6:58
14

Found one that works

function MYMODULE_views_pre_render(&$view) {
  if ($view->name == 'menu') {
    $view->build_info['title'] = "TEST TITLE";
  }
}
2
  • 1
    This 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).
    – neuquen
    Commented Jan 16, 2015 at 20:25
8

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";
}
2
  • 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.
    – neuquen
    Commented Jan 16, 2015 at 20:20
5

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');
  }
}
2

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.

0
1

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;
  } 
}
1
  • Thanks, but I dont see a "title" variable in $view with this function.
    – giorgio79
    Commented Jun 5, 2012 at 12:16
1

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';
  }
}
0

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.

1
  • Much appreciated, no luck so far. I also have Page Title installed, and that somehow seem to override every other settings.
    – giorgio79
    Commented Jun 5, 2012 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.