Skip to content

Instantly share code, notes, and snippets.

@mattschaff
Last active April 4, 2019 13:56
Show Gist options
  • Save mattschaff/132636fe9a6f3bed64e04904f568d7ae to your computer and use it in GitHub Desktop.
Save mattschaff/132636fe9a6f3bed64e04904f568d7ae to your computer and use it in GitHub Desktop.
Drupal 8: Dynamically set page title of view page display
<?php
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_post_render().
*/
function my_module_views_post_render(ViewExecutable $view, &$output) {
if ($view->id() == 'view_machine_name') {
// Set title based on the user name of the current user profile.
// Note: This view is only set to display on the /user path.
$current_path = \Drupal::service('path.current')->getPath();
$path_items = explode('/', $current_path);
$user = User::load($path_items[2]);
$view->setTitle($user->getAccountName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment