* 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
+ );
}
}
}
/**
+ * Unknown tokens are removed at the very end.
+ *
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCreateDocumentUnknownTokens(): void {
$activity = $this->activityCreate();
- $html_message = 'Unknown token: {activity.something_unknown}';
+ $html_message = 'Unknown token: ';
$form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
$output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
- // Unknown tokens should be left alone
$this->assertEquals($html_message, $output[0]);
}
}
}
+ /**
+ * Test that unresolved tokens are not causing a fatal error in smarty.
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ public function testUnresolvedTokens(): void {
+ CRM_Core_BAO_MessageTemplate::renderTemplate([
+ 'messageTemplate' => [
+ 'msg_text' => '{contact.blah}',
+ ],
+ ])['text'];
+ }
+
/**
* Hook to advertise tokens.
*