Skip to content

Commit

Permalink
Merge pull request #27204 from eileenmcnaughton/br2nl
Browse files Browse the repository at this point in the history
Pass html text through a formatter when translating it to tokens
  • Loading branch information
colemanw authored Sep 5, 2023
2 parents 5f90b41 + 79af442 commit 2aabd2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Civi/Token/TokenRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ public function customToken($entity, $customFieldID, $entityID) {
'id' => $entityID,
]);
$fieldValue = \CRM_Utils_Array::value($customFieldName, $record, '');

$originalValue = $fieldValue;
// format the raw custom field value into proper display value
if (isset($fieldValue)) {
$fieldValue = (string) \CRM_Core_BAO_CustomField::displayValue($fieldValue, $customFieldID);
}
// This is a bit of a clumsy wy of detecting a link field but if you look into the displayValue
// function you will understand.... By assigning the url as a plain token the text version can
// use it as plain text (not html re-converted which kinda works but not in subject lines)
if (is_string($fieldValue) && is_string($originalValue) && strpos($fieldValue, '<a href') !== FALSE && strpos($originalValue, '<a href') === FALSE) {
$this->format('text/plain')->tokens($entity, $customFieldName, $originalValue);
}

return $this->format('text/html')->tokens($entity, $customFieldName, $fieldValue);
}
Expand Down Expand Up @@ -292,7 +298,8 @@ public function fill($format = NULL) {
foreach ($htmlTokens as $entity => $values) {
foreach ($values as $field => $value) {
if (!$value instanceof \DateTime && !$value instanceof Money) {
$value = html_entity_decode(strip_tags($value));
// rtrim removes trailing lines from <p> tags.
$value = rtrim(\CRM_Utils_String::htmlToText($value));
}
if (!isset($textTokens[$entity][$field])) {
$textTokens[$entity][$field] = $value;
Expand Down
9 changes: 8 additions & 1 deletion tests/phpunit/CRM/Utils/TokenConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,14 @@ public function testEscaping(): void {

// The `description` does allow HTML. Any funny characters are filtered out of text.
$messages['event_text'] = 'You signed up for this event: {event.title}: {event.description}';
$expected['event_text'] = 'You signed up for this event: The Webinar: Some online webinar thingy. Attendees will need to install the TeleFoo app.';
$expected['event_text'] = 'You signed up for this event: The Webinar: Some online webinar thingy.
Attendees will need to install the TeleFoo [1] app.
Links:
------
[1] http://telefoo.example.com';

$messages['event_html'] = '<p>You signed up for this event:</p> <h3>{event.title}</h3> {event.description}';
$expected['event_html'] = '<p>You signed up for this event:</p> <h3>The Webinar</h3> <p>Some online webinar thingy.</p> <p>Attendees will need to install the <a href="http://telefoo.example.com">TeleFoo</a> app.</p>';
Expand Down
8 changes: 5 additions & 3 deletions tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Civi\Token;

use Brick\Money\Context\DefaultContext;
use Brick\Money\Money;
use Civi\Api4\Website;
use Civi\Token\Event\TokenRegisterEvent;
use Civi\Token\Event\TokenValueEvent;
Expand Down Expand Up @@ -462,7 +464,7 @@ public function testFull(): void {
$this->assertEquals(1, $this->counts['onEvalTokens']);
}

public function getFilterExamples() {
public function getFilterExamples(): array {
$exampleTokens = [
// All the "{my_text.*}" tokens will be treated as plain-text ("text/plain").
'my_text' => [
Expand All @@ -477,7 +479,7 @@ public function getFilterExamples() {
'and_such' => '<strong>testing &amp; such</strong>',
],
'my_currencies' => [
'amount' => \Brick\Money\Money::of(123, 'USD', new \Brick\Money\Context\DefaultContext()),
'amount' => Money::of(123, 'USD', new DefaultContext()),
'currency' => 'EUR',
'locale' => 'fr_FR',
],
Expand Down Expand Up @@ -522,7 +524,7 @@ public function getFilterExamples() {
$testCases['TextMessages with HtmlData'] = [
'text/plain',
[
'This is {my_rich_text.and_such}...' => 'This is testing & such...',
'This is {my_rich_text.and_such}...' => 'This is TESTING & SUCH...',
'This is {my_rich_text.and_such|lower}...' => 'This is testing & such...',
'This is {my_rich_text.and_such|upper}!' => 'This is TESTING & SUCH!',
],
Expand Down

0 comments on commit 2aabd2f

Please sign in to comment.