Problem/Motivation
When sending an email in the context of a JSON:API request the response is the error: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\jsonapi\ResourceResponse.
Steps to reproduce
Any email sent in the context of JSON:API request trows that error. Example:
function bloom_custom_node_insert(\Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() === 'match') {
$match = $entity;
$ad_1 = $match->field_ad_1->entity;
// Send email on match
/** @var \Drupal\easy_email\Service\EmailHandlerInterface $email_handler */
$email_handler = \Drupal::service('easy_email.handler');
// Ad_1
$email_1 = $email_handler->createEmail([
'type' => 'new_match',
]);
$email_1->setCreatorId($ad_1->getOwner()->id());
$email_1->setRecipientIds([$ad_1->getOwner()->id()]);
$status = $email_handler->sendEmail($email_1);
}
}
Comments
Comment #2
introfini commentedI traced the problem to this line
$this->tokenEvaluator->evaluateTokens($email)in easy_email/src/Service/EmailHandler.php but couldn't find more than that. So it's something related to token processing since there's a lot of rendering taking place there.Has a quick fix (inspired by this article) if I wrap the email sending inside this, it works:
Comment #3
introfini commentedThis fixes the issue:
Do you think it's a valid solution?
Comment #5
introfini commentedHere's the patch...
Comment #8
zengenuity commentedLooks good to me. I had to do the same thing for the body text field previously.
I committed this to 8.x-1.x and 2.0.x.
One thing though, that I noticed after I committed, was that we are also calling
$this->tokenEvaluator->evaluateTokens($email);in the preview function. Since you're getting this error on your site, can you check if preview is working on that template for a similar set of input field values? It seems like it should have the same problem, but I can't easily test it since I did not have the original problem.Comment #9
zengenuity commented