0

This is the problem at hand: the first time I access a page after a cache rebuild, the title of said page is displayed correctly. But when I refresh (or access it later after navigating other pages) the title completely dissappears.

My page-title.html.twig is as follows

    {% if title %}
      <h2{{ title_attributes.addClass('page-header') }}>{{ title }}</h2>
    {% endif %}

and it doesn't pass the condition check.

If I debug with a breakpoint in HOOK_preprocess_page_title() I see that the title is null. But if I debug HOOK_preprocess_page() the title has the correct markup. So it would seem that somewhere in between those two hooks, the title is somehow being deleted/ignored.

Any ideas on why this is happening or where I could look for further debugging?

I'm currently using this workaround (included in case anyone else is looking for a solution to this problem)

    function HOOK_preprocess_page_title(&$variables) {
      if (is_null($variables['title'])) {
        $request = \Drupal::request();
        $route_match = \Drupal::routeMatch();
        if (isset($request) && isset($route_match)) {
          $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
          if(!empty($title)) {
            $variables['title'] = Drupal\Core\Render\Markup::create('<span>' . Html::escape($title) . '</span>');
          }
        }
      }
    }

but I would rather find the root cause for this behaviour and fix it.

  • 2
    The page title render element gets the title from the title block, which gets the title from the display variant, which gets the title from HtmlRenderer::prepare(). So the question is, what have you changed to break this standard behaviour? – 4k4 Aug 19 at 10:08
  • I don't know. But if it were something I did, wouldn't it also happen on first load? What I find most intriguing is that it happens only when the page is cached. – eloyra Aug 19 at 10:21
  • Yes, this seems odd, when the page is served from cache, the preprocess hooks you've mentioned are not called at all. Since your question is mainly about how to debug this, you might want to start with setting a breakpoint in HtmlRenderer::prepare() – 4k4 Aug 19 at 11:35
  • That method also doesn't get called at all when the page is cached as far as I can tell. Tried setting multiple breakpoints across the function. The first time it stops but second and successive times it bypasses the method. – eloyra Aug 19 at 11:59
  • This is then the behaviour you would expect, unlike what you've found out about the preprocess hooks. – 4k4 Aug 19 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.