From df03b9a323d6ef6dabd4de4fa803a1a9f41ebcba Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Mon, 17 Jan 2022 11:49:17 +0100 Subject: [PATCH] [dev/core#3029] Throw and catch exception instead of risking a TypeError when evaluating tokens for non-existent custom fields --- CRM/Core/EntityTokens.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 563cc10a49..1e5fc68277 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -496,6 +496,8 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { * @param int $id * * @return string + * + * @throws \CRM_Core_Exception */ protected function getCustomFieldName(int $id): string { foreach ($this->getTokenMetadata() as $key => $field) { @@ -503,6 +505,9 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return $key; } } + throw new CRM_Core_Exception( + "A custom field with the ID {$id} does not exist" + ); } /** @@ -515,9 +520,14 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { */ protected function getCustomFieldValue($entityID, string $field) { $id = str_replace('custom_', '', $field); - $value = $this->prefetch[$entityID][$this->getCustomFieldName($id)] ?? ''; - if ($value !== NULL) { - return CRM_Core_BAO_CustomField::displayValue($value, $id); + try { + $value = $this->prefetch[$entityID][$this->getCustomFieldName($id)] ?? ''; + if ($value !== NULL) { + return CRM_Core_BAO_CustomField::displayValue($value, $id); + } + } + catch (CRM_Core_Exception $exception) { + return NULL; } } -- 2.25.1