Use BubbleableMetadata as an argument to your token replace function!

I had been using samlauth module, and I wrote another module using tokens on hook_user_login.

The combination caused a problem because I didn't add a BubbleableMetadata object to the end of the \Drupal::token->replace() method.

The error was:

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\Routing\TrustedRedirectResponse.
in Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
(line 154 of /var/www/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php

 

Example: 

  $bubbleable_metadata = new BubbleableMetadata();
  $output = $token_service->replace($config->get('format'), ['event-log' => $row], [], $bubbleable_metadata);

Just thought this should exist somewhere to anyone else with this issue.


Leave us a comment!