I want to be able to change the main page title based on some value that I get in a views preprocess function.

The View is displayed as a page.

function MYTHEME_preprocess_views_view(&$variables) {

  // Can I set the page title from here?

The title I am trying to override is the title printed by the /templates/content/page-title.html.twig below:

 * @file
 * Theme override for page titles.
 * Available variables:
 * - title_attributes: HTML attributes for the page title element.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title: The page title, for use in the actual content.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
{{ title_prefix }}
{% if title %}
  <h1{{ title_attributes }}>{{ title }}</h1>
{% endif %}
{{ title_suffix }}
  • @leymannx MYTHEME_preprocess_page_title is the only function in which I was able to modify this title. But then I don't have access to the view. As I said in my question, I need to change the title based on some value I define in MYTHEME_preprocess_views_view.
    – MrUpsidown
    Aug 14, 2019 at 12:36
  • I am able to do it in hook_views_post_render but now I am thinking I need to use AJAX for my view and the title replacement will need to occur in an AJAX callback of some sort... Thanks for the help anyway.
    – MrUpsidown
    Aug 14, 2019 at 13:00

2 Answers 2


I don't think hook_preprocess_views_view() or hook_preprocess_page_title() is good ways in your case. You can use hook_views_pre_render

 * Implements hook_views_pre_render().
function MYMODULE_views_pre_render(ViewExecutable $view) {

  if ($view->id() == 'VIEW_ID') {
    $arg = $view->args; // Argument from view. 
    $default_title = $view->getTitle(); // Get view title.
    $view->setTitle('New title'); // Set new title

All variables you need in here.


Easiest to preprocess the page title is MYTHEME_preprocess_page_title(&$variables) and then use $variables['title'] = 'New Title'; to set a new page title.

Inside there you can load your view by its ID and do whatever you need to do to have the title manipulation fired.

use Drupal\views\Views;

 * Implements hook_preprocess_HOOK().
function MYTHEME_preprocess_page_title(&$variables) {

  // Replace MYVIEWID with you view ID.
  $view_id = 'MYVIEWID';

  if (\Drupal::routeMatch()->getParameter('view_id') === 'content') {

    $view = Views::getView($view_id);

    // ksm($view);

    // Do whatever you need to do and then set the page title.
    $variables['title'] = 'New Title';
  • Ok... this looks a bit like a hack though...
    – MrUpsidown
    Aug 14, 2019 at 13:15
  • Yeah? I would use that in production. :)
    – leymannx
    Aug 14, 2019 at 13:20
  • Would you really? What about path aliases, languages, etc? I haven't tested your solution yet but I am just wondering...
    – MrUpsidown
    Aug 14, 2019 at 13:23
  • 1
    @MrUpsidown, about path aliases or languages, you can get the view id of a views page from the route, no matter how you did reach it: \Drupal::routeMatch()->getParameter('view_id');
    – 4uk4
    Aug 14, 2019 at 15:23

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.