Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Created April 12, 2018 01:59
Show Gist options
  • Select an option

  • Save Greg-Boggs/9282df4347a522e5a19b77730c4f9012 to your computer and use it in GitHub Desktop.

Select an option

Save Greg-Boggs/9282df4347a522e5a19b77730c4f9012 to your computer and use it in GitHub Desktop.
Get the title of any page in Drupal 8
<?php
$title = $this->titleResolver->getTitle($route_request, $route_match->getRouteObject());
// Many paths return a translatable markup object.
if ($title instanceof TranslatableMarkup) {
// Sets the title to the translated string.
$title = $title->render();
}
// Other paths, such as admin/structure/menu/manage/main, will
// return a render array suitable to render using core's XSS filter.
elseif (is_array($title) && array_key_exists('#markup', $title) && array_key_exists('#allowed_tags', $title)) {
// Sets the title to the XSS filtered string.
$title = Xss::filter($title['#markup'], $title['#allowed_tags']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment