From d2c423f2bdb004bb52a05883baa9c41c2baa420f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 3 Aug 2022 17:01:47 +1200 Subject: [PATCH] Avoid extra queries when prefix_id:label is a token Because the value was not populating the array it kept getting it over & over --- CRM/Contact/Tokens.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/Tokens.php b/CRM/Contact/Tokens.php index 731b2f330ceb..7af3c1c3f90c 100644 --- a/CRM/Contact/Tokens.php +++ b/CRM/Contact/Tokens.php @@ -284,7 +284,7 @@ public function evaluateLegacyHookTokens(TokenValueEvent $e): void { * * @param \Civi\Token\Event\TokenValueEvent $e * - * @throws TokenException + * @throws \Civi\Token\TokenException * @throws \CRM_Core_Exception */ public function onEvaluate(TokenValueEvent $e) { @@ -350,6 +350,15 @@ protected function getFieldValue(TokenRow $row, string $field) { if (isset($row->context[$entityName][$possibility])) { return $row->context[$entityName][$possibility]; } + if ($this->isPseudoField($possibility)) { + // If we have a name or label field & already have the id loaded then we can + // evaluate from that rather than query again. + $split = explode(':', $possibility); + if (isset($row->context[$entityName][$split[0]])) { + $row->context[$entityName][$possibility] = $this->getPseudoValue($split[0], $split[1], $row->context[$entityName][$split[0]]); + return $row->context[$entityName][$possibility]; + } + } } $contactID = $this->getFieldValue($row, 'id'); if ($contactID) {