Skip to content

Commit

Permalink
dev/core#2814 fix tokenCompat to be consistent with unresolved tokens
Browse files Browse the repository at this point in the history
This fixes the tokenCompat subscriber to replace unresolved tokens with a
blank string in a consistent way.

Prior to this it would crash if smarty was enabled but not all tokens
were resolved & print unresolved tokens if smarty was not enabled.

The inconsistencies appear to be due to 'separate evolution' rather than '*reasons*'
  • Loading branch information
eileenmcnaughton committed Sep 22, 2021
1 parent 3ed153b commit e2d052a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 9 additions & 12 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,18 @@ protected function getFieldMetadata(): array {
* Apply the various CRM_Utils_Token helpers.
*
* @param \Civi\Token\Event\TokenRenderEvent $e
*
* @throws \CRM_Core_Exception
*/
public function onRender(TokenRenderEvent $e): void {
$isHtml = ($e->message['format'] === 'text/html');
$useSmarty = !empty($e->context['smarty']);

if (!empty($e->context['contact'])) {
// @todo - remove this - it simply removes the last unresolved tokens before
// they break smarty.
// historically it was only called when context['contact'] so that is
// retained but it only works because it's almost always true.
$remainingTokens = array_keys(\CRM_Utils_Token::getTokens($e->string));
if (!empty($remainingTokens)) {
$e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $remainingTokens);
$remainingTokens = \CRM_Utils_Token::getTokens($e->string);

if ($remainingTokens) {
foreach ($remainingTokens as $part1 => $part2) {
$e->string = preg_replace(
'/(?<!\{|\\\\)\{' . $part1 . '\.([\w]+(:|\.)?\w*(\-[\w\s]+)?)\}(?!\})/',
'',
$e->string
);
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ public function hookTokenValues(array &$details): void {
}
}

/**
* Test that unresolved tokens are removed in the end.
*/
public function testUnresolvedTokens(): void {
CRM_Core_BAO_MessageTemplate::renderTemplate([
'messageTemplate' => [
'msg_text' => '{contact.blah}',
],
])['text'];
}

/**
* Hook to advertise tokens.
*
Expand Down

0 comments on commit e2d052a

Please sign in to comment.