Created
November 25, 2022 14:20
-
-
Save MatthieuScarset/1c4d1c5230be882c68ac55e377978e69 to your computer and use it in GitHub Desktop.
Drupal - Custom view page title
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Custom view page title | |
// @see https://www.drupal.org/node/2067859#comment-14675179 | |
/** | |
* Implements hook_views_post_render(). | |
*/ | |
function mymodule_views_post_render(Drupal\views\ViewExecutable $view) { | |
if ($view->element['#view_id'] == 'id_of_view') { | |
if ($view->element['#display_id'] == 'id_of_display') { | |
$title = 'test'; | |
$view->setTitle($title); // Sets h1 | |
$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject(); | |
$route->setDefault('_title_callback', function() use ($title) { | |
return $title; // sets <head><title> | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment