Problem/Motivation
Coming from
✨
Submodule to override the (visible) page title ("metatag_page_title")
Active
, we tried to find a central point to override the page heading (h1), for SEO purposes. But currently there is no way to override the raw page heading before it gets rendered through the theme.
You could argue, that it would be possible to override the title through either hook_preprocess_page_title
or through preprocessing the "page_title_block", but at that point the title is already rendered through twig, and we get markup along the lines of
<span class="field field--name-title field--type-string field--label-hidden">Test</span>
(when using "claro")
So if we would override the title in "hook_preprocess_page_title", we would lose the child span and its classes in this case.
What I want is to simply override the actual heading value without loosing any theme classes / elements. So before the value is passed to the theme.
This seemingly happens in /lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
prepare()
We already tried to implement our own titleResolver with an overriden "getTitle()" method (which gives the raw h1) and replace the original titleResolver in the core.services.yml, but the title from there is only used as a backup, if $main_content['#title']
is NULL.
We basically need to be able to overwrite the title, before it gets rendered in HtmlRenderer between line 230 and 243.
Steps to reproduce
Proposed resolution
Implement "hook_page_heading_alter(&$request, &$route_match, $title)" as a central point to override the raw page heading (h1) before it gets rendered through twig.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Miscellaneous
This could also help the following contrib modules / issues:
and could be seen as the successor to Drupal 7's "drupal_set_title()"