Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Created November 25, 2022 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthieuScarset/1c4d1c5230be882c68ac55e377978e69 to your computer and use it in GitHub Desktop.
Save MatthieuScarset/1c4d1c5230be882c68ac55e377978e69 to your computer and use it in GitHub Desktop.
Drupal - Custom view page title
<?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