From c58e19e8c7833194c6549f0b385b70cf1e0ff211 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 22 Nov 2022 16:29:26 -0500 Subject: [PATCH] default token filter --- Civi/Token/TokenProcessor.php | 8 ++++++++ tests/phpunit/Civi/Token/TokenProcessorTest.php | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 1ed89df5fc..a5434e138d 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -508,6 +508,14 @@ class TokenProcessor { return $value; } + case 'default': + if (!$value) { + return $filter[1]; + } + else { + return $value; + } + default: throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES)); } diff --git a/tests/phpunit/Civi/Token/TokenProcessorTest.php b/tests/phpunit/Civi/Token/TokenProcessorTest.php index b606ca4c28..d3eff21d4c 100644 --- a/tests/phpunit/Civi/Token/TokenProcessorTest.php +++ b/tests/phpunit/Civi/Token/TokenProcessorTest.php @@ -350,9 +350,11 @@ class TokenProcessorTest extends \CiviUnitTestCase { 'This is {foo_bar.whiz_bang|upper}!' => 'This is SOME TEXT!', 'This is {foo_bar.whiz_bang|boolean}!' => 'This is 1!', 'This is {foo_bar.whiz_bop|boolean}!' => 'This is 0!', + 'This is {foo_bar.whiz_bang|default:"bang"}.' => 'This is Some Text.', + 'This is {foo_bar.whiz_bop|default:"bop"}.' => 'This is bop.', ]; - // We expect 5 messages to be parsed 2 times each - ie 10 times. - $expectExampleCount = 10; + // We expect 7 messages to be parsed 2 times each - ie 14 times. + $expectExampleCount = 14; $actualExampleCount = 0; foreach ($exampleMessages as $inputMessage => $expectOutput) { -- 2.25.1