7

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.

  • There is now Page Title contrib module that offers Views integration as well. – Refineo May 8 '12 at 5:19
  • You can achieve it with the Metatag module. This module allows you to automatically provide structured metadata. – heilop Jan 16 '13 at 15:30
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');
  }
}
  • 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 Sep 27 '11 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. – marcvangend Sep 27 '11 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 Jul 25 '18 at 6:58
13

Found one that works

function MYMODULE_views_pre_render(&$view) {
  if ($view->name == 'menu') {
    $view->build_info['title'] = "TEST TITLE";
  }
}
  • 1
    This didn't work for me when I had render caching enabled – David Meister Nov 7 '13 at 1:20
  • Caching was my problem too. Using hook_views_pre_view solved it for me (See the response from @MPD). – Keven Jan 16 '15 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";
}
  • This hook worked for me, even with render caching enabled, but I had to also add drupal_set_title() for my use-case – David Meister Nov 7 '13 at 1:51
  • In my case I had to change $view->display['default']->handler->options['title'] rather than the $view->current_display. – Keven Jan 16 '15 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.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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