Allow altering the page title (text) before it is passed to the theming layer

Created on 7 August 2024, 5 months ago

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()"

📌 Task
Status

Active

Version

11.0 🔥

Component
Render 

Last updated about 8 hours ago

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Grevil
  • 🇩🇪Germany Anybody Porta Westfalica

    Thank you very much @Grevil and a BIG +1!
    I've been running into this struggle again and again and never found a way to cleanly alter the page title before it became a rendered string reliably.

    I'm not yet sure, how this can be done, but for modules that need to alter the title it's really had to work with the existing structure, as the title can be:

    • Object of class Markup
    • A render array with whatever structure
    • A string with the title

    So you can never rely on, where to find the title string. And I'm very unsure if it's really useful to have this freedom.

    What I'd expect would be to have a clear string source for the page title that can be altered and anything else around can be done by the theme level on top... :/

  • 🇩🇪Germany Anybody Porta Westfalica
Production build 0.71.5 2024