Redering outside of a render context

Created on 16 November 2023, 7 months ago
Updated 2 March 2024, 4 months ago

I came across this in my project where I use Drupal headless and distribute data through GraphQL.

I wanted to add the breadcrumb to a specific endpoint by injecting the BreadcrumbManager service.

...
$breadcrumb = $this->breadcrumbManager->build($routeMatch);
...

This threw the following error:

LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\Core\Cache\CacheableJsonResponse.

Diving into the code, I found the issue in EasyBreadcrumbBuilder::setRouteContextFromRouteMatch()

if ($url && $request = $this->getRequestForPath($url->toString(), []))

Changing this to the following seems to work:

if ($url && $request = $this->getRequestForPath($url->toString(TRUE)->getGeneratedUrl(), []))

Is this a correct assumption and is this the correct way to tackle this issue?

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇳🇱Netherlands robert-os

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024