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) {