Drupal 8 and higher

To change the title of a page view, you actually need to change both the view title (which will change e.g. the <body><h1> content) and the default title of the current request's matching route (which will change e.g. the <head><title> content.)

Since this change record, you should use Drupal's CurrentRouteMatch service to find the current route, not any Symfony code. Either:

  1. procedural code: call \Drupal::routeMatch(), or:
  2. object-oriented code: inject the current_route_match service (preferred).

For the purposes of just changing a view's title, this can be done procedurally in a hook_views_post_render() in a custom module's .module file:


use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_post_render().
 */
function mymodule_views_post_render(ViewExecutable $view) {
  $title = "My custom title";

  // Set the view title.
  $view->setTitle($title);
  // Set the route title.
  $route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
  $route->setDefault('_title', $title);
}

Drupal 7

If you have a view and you want to be able to programmatically change the title of, you can do it by implementing hook_views_pre_render in your custom module:

function mymodule_views_pre_render(&$view) {
  if ($view->name == 'my_view_name' && $view->current_display == 'my_display_id') {
    // Here you can do any php you want to get the title you need for your view.
    $view->build_info['title'] = "My custom title!";
  }
}

If you are altering the query and you have argument substitutions affecting the title, you can change these in hook_views_query_alter:

function mymodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'my_view_name' && $view->current_display == 'my_display_id') {
      // First substitution - you may want !1 or %2, etc
      $view->build_info['substitutions']['%1'] = $new_substitution;
    }
}

Views Displayed as Pages

Use this:

drupal_set_title('My custom title!');

Instead of:

$view->build_info['title'] = "My custom title!";

Comments

ChrisGrewe’s picture

If it helps anyone else, I was having issues with the drupal_set_title portion of this (my module sets the breadcrumbs and the page title), and switching back to setting the $view->build_info['title'] variable rather than using the function fixed the issue.

xandeadx’s picture

for Views 3:

/**
 * Implements hook_views_pre_view().
 */
function MODULENAME_views_pre_render($view) {
  if ($view->name == 'my_view_name') {
    if ($view->current_display == 'my_display_name') {
      $view->set_title('my new title');
    }
  }
}
AlexKh’s picture

Thanks a lot

Phil Wolstenholme’s picture

Here's a Drupal 8 approach to use in a custom module. This seems to be necessary to get a contextual filter value to override the title of a views page display.


<?php
/**
 * Implements hook_views_post_render().
 */
function MODULE_NAME_views_post_render($view) {
  if ($view->id() !== 'VIEW_ID' && $view->getDisplay()->getPluginId() !== 'page') {
    return;
  }
  $request = \Drupal::request();
  if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
    $new_title = $view->getTitle();
    $route->setDefault('_title', $new_title);
  }
}
abu-zakham’s picture

/**
 * Implements hook_views_post_render().
 */
function my_module_views_post_render($view) {
   if ($view->id() == 'VIEW_ID' && $view->current_display == 'DISPLAY_ID') {
    // Set the view title.
    $view->setTitle('NEW TITLE');
   }
}
Dhruv Panchal’s picture

You can set views page title programmatically by using below hook in modules.

function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {

if ($view->name == 'VIEW_MACHINE_NAME') {

$view->display[$view->current_display]->display_options["title"] =
$view->display[$view->current_display]->handler->options["title"] =
$view->human_name .' - '.$_GET['field_video_by_event_value'];
}

}

Sana.Neyazi’s picture

/**
* Implements hook_views_pre_view().
*/
function MODULENAME_views_pre_render($view) {
if ($view->name == 'my_view_name') {
if ($view->current_display == 'my_display_name') {
$view->set_title('new title');
}
}
}

serverjohn’s picture

For some reason the code from the article example does not work:

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$route->setDefault('_title', 'New Title');

It will not change the <head><title> but it seems like it should.

This works to set the title in the body of the page.

$view->setTitle($title);

Any ideas? My head is spinning.

marcoka’s picture

Thats true. Does not set the tag in drupal 9.4.0 neither.

edysmp’s picture

Working for me in 9.4.7

serverjohn’s picture

How do I programmatically set the view title?

It is very similar to the example but it uses the _title_callback function.

ressa’s picture

If only the title changes on the page (i.e. H1) and not the title in the head of the document (i.e Custom title), check if Metatag is overriding it. I wonder if Views titles cannot be overridden and work together with Metatag, as discussed in #2895804: Views meta tags by argument? See also How do I programmatically set the view title?