diff --git a/CRM/Activity/Tokens.php b/CRM/Activity/Tokens.php index 94cbd4807c2d..7a0ba27d4096 100644 --- a/CRM/Activity/Tokens.php +++ b/CRM/Activity/Tokens.php @@ -110,8 +110,8 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } - elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $field, $actionSearchResult->entity_id); + elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } else { $row->tokens($entity, $field, ''); diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index 62dcbe5e516e..40a5c832a9be 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -136,8 +136,8 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe elseif (isset($aliasTokens[$field])) { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); } - elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $field, $actionSearchResult->entity_id); + elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } else { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index 923a5414d5ef..e947ade45f79 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -142,8 +142,8 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } - elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $field, $actionSearchResult->entity_id); + elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } else { $row->tokens($entity, $field, ''); diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index 26adb29f485b..7fe6496a292d 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -98,8 +98,8 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe elseif (isset($actionSearchResult->$field)) { $row->tokens($entity, $field, $actionSearchResult->$field); } - elseif (\CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $field, $actionSearchResult->entity_id); + elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { + $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } else { $row->tokens($entity, $field, ''); diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 7a2ce239a7c3..ccd70813bf00 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -66,7 +66,10 @@ public function onEvaluate(TokenValueEvent $e) { if (!empty($messageTokens['contact'])) { foreach ($messageTokens['contact'] as $token) { if (\CRM_Core_BAO_CustomField::getKeyID($token)) { - $row->customToken('Contact', $token, $contactId); + $contact[$token] = civicrm_api3('Contact', 'getvalue', array( + 'return' => $token, + 'id' => $contactId, + )); } } } @@ -112,12 +115,12 @@ public function onRender(TokenRenderEvent $e) { $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain::getDomain(), $isHtml, $e->message['tokens'], $useSmarty); if (!empty($e->context['contact'])) { - \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, FALSE); - - $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], FALSE, $useSmarty); + $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], TRUE, $useSmarty); // FIXME: This may depend on $contact being merged with hook values. $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty); + + \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty); } if ($useSmarty) { diff --git a/Civi/Token/TokenRow.php b/Civi/Token/TokenRow.php index 1a3033a9aca1..4f186d3d5c1e 100644 --- a/Civi/Token/TokenRow.php +++ b/Civi/Token/TokenRow.php @@ -130,16 +130,23 @@ public function tokens($a = NULL, $b = NULL, $c = NULL) { * Update the value of a custom field token. * * @param string $entity - * @param string $fieldName - * @param string $entityID + * @param int $customFieldID + * @param int $entityID * @return TokenRow */ - public function customToken($entity, $fieldName, $entityID) { + public function customToken($entity, $customFieldID, $entityID) { + $customFieldName = "custom_" . $customFieldID; $fieldValue = civicrm_api3($entity, 'getvalue', array( - 'return' => $fieldName, + 'return' => $customFieldName, 'id' => $entityID, )); - return $this->tokens($entity, $fieldName, $fieldValue); + + // format the raw custom field value into proper display value + if ($fieldValue) { + $fieldValue = \CRM_Core_BAO_CustomField::displayValue($fieldValue, $customFieldID); + } + + return $this->tokens($entity, $customFieldName, $fieldValue); } /**