Drupal - How do I show the current node title in page.html.twig OR in node.html.twig?
The node is already available in the page template thanks to template_preprocess_page
, so there's no need for the preprocess function. You can output the title with:
{{ node.label }}
The page title itself is a block, so if you don't need it any more just remove it from the region at /admin/structure/block. No need to copy the template to your theme or alter the original.
You can add below codes to top of node.html.twig
in templates folder of your theme :
{% if not page %}
<h2{{ title_attributes }}>
<a href="{{ url }}" rel="bookmark">{{ label }}</a>
</h2>
{% endif %}
{% if page %}
<h2{{ title_attributes }}>
{{ label }}
</h2>
{% endif %}
The Drupal 8 code for doing what the code you shown should do is the following one.
function theme_preprocess_page(&$variables) {
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
if ($route_match->getParameter('node')) {
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
$variables['my_node_title'] = $page_title;
}
}