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:
- procedural code: call
\Drupal::routeMatch()
, or: - 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
If it helps...
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.
for Views 3:
for Views 3:
Great
Thanks a lot
Here's a Drupal 8 approach to
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.
Drupal 8.3.6 and higher
Programmtically changes views page title.
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'];
}
}
/**
/**
* 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');
}
}
}
Anyone know why this doesn't work?
For some reason the code from the article example does not work:
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.
Thats true. Does not set the
Thats true. Does not set the tag in drupal 9.4.0 neither.
Working for me in 9.4.7
Working for me in 9.4.7
This seems to work.
How do I programmatically set the view title?
It is very similar to the example but it uses the _title_callback function.
Metatag
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?