From 18c017c898928391c0446b2d34e0d5b7c2b4667d Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Sun, 4 Jun 2017 12:45:09 +0530 Subject: [PATCH] CRM-20682: Include human readable contribution's custom field label in token widget for Thankyou letter --- CRM/Activity/Tokens.php | 2 +- CRM/Contribute/Tokens.php | 2 +- CRM/Core/SelectValues.php | 4 ++-- CRM/Event/Tokens.php | 2 +- CRM/Member/Tokens.php | 2 +- CRM/Utils/Token.php | 22 +++++++++++++++++++++- Civi/Token/AbstractTokenSubscriber.php | 16 ---------------- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/CRM/Activity/Tokens.php b/CRM/Activity/Tokens.php index 7a0ba27d40..eb5ad34b82 100644 --- a/CRM/Activity/Tokens.php +++ b/CRM/Activity/Tokens.php @@ -57,7 +57,7 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber { 'details' => ts('Activity Details'), 'activity_date_time' => ts('Activity Date-Time'), ), - $this->getCustomTokens('Activity') + CRM_Utils_Token::getCustomFieldTokens('Activity') )); } diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index 40a5c832a9..441906fa7e 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -85,7 +85,7 @@ class CRM_Contribute_Tokens extends \Civi\Token\AbstractTokenSubscriber { $tokens['source'] = ts('Contribution Source'); $tokens['status'] = ts('Contribution Status'); $tokens['type'] = ts('Financial Type'); - $tokens = array_merge($tokens, $this->getCustomTokens('Contribution')); + $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution')); parent::__construct('contribution', $tokens); } diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 22416b15d2..ece069395c 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -559,7 +559,7 @@ class CRM_Core_SelectValues { * @return array */ public static function contributionTokens() { - return array( + return array_merge(array( '{contribution.contribution_id}' => ts('Contribution ID'), '{contribution.total_amount}' => ts('Total Amount'), '{contribution.fee_amount}' => ts('Fee Amount'), @@ -583,7 +583,7 @@ class CRM_Core_SelectValues { //'{contribution.address_id}' => ts('Address ID'), '{contribution.check_number}' => ts('Check Number'), '{contribution.campaign}' => ts('Contribution Campaign'), - ); + ), CRM_Utils_Token::getCustomFieldTokens('contribution', TRUE)); } /** diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index e947ade45f..423dd79a1d 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -61,7 +61,7 @@ class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { 'contact_phone' => ts('Event Contact (Phone)'), 'balance' => ts('Event Balance'), ), - $this->getCustomTokens('Event') + CRM_Utils_Token::getCustomFieldTokens('Event') )); } diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index 7fe6496a29..cc664cfec4 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -54,7 +54,7 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber { 'status' => ts('Membership Status'), 'type' => ts('Membership Type'), ), - $this->getCustomTokens('Membership') + CRM_Utils_Token::getCustomFieldTokens('Membership') )); } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 257a1b1893..4026670699 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1517,7 +1517,8 @@ class CRM_Utils_Token { $key = 'contribution'; if (self::$_tokens[$key] == NULL) { self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'), - array('campaign', 'financial_type') + array('campaign', 'financial_type'), + self::getCustomFieldTokens('Contribution') )); } } @@ -1797,6 +1798,25 @@ class CRM_Utils_Token { ); } + /** + * Get all custom field tokens of $entity + * + * @param string $entity + * @param bool $usedForTokenWidget + * + * @return array $customTokens + * return custom field tokens in array('custom_N' => 'label') format + */ + public static function getCustomFieldTokens($entity, $usedForTokenWidget = FALSE) { + $customTokens = array(); + $tokenName = $usedForTokenWidget ? "{contribution.custom_%d}" : "custom_%d"; + foreach (CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) { + $customTokens[sprintf($tokenName, $id)] = $info['label']; + } + + return $customTokens; + } + /** * Formats a token list for the select2 widget * diff --git a/Civi/Token/AbstractTokenSubscriber.php b/Civi/Token/AbstractTokenSubscriber.php index 1164032de8..450955bfd5 100644 --- a/Civi/Token/AbstractTokenSubscriber.php +++ b/Civi/Token/AbstractTokenSubscriber.php @@ -117,22 +117,6 @@ abstract class AbstractTokenSubscriber implements EventSubscriberInterface { } } - /** - * Get all custom field tokens of $entity - * - * @param string $entity - * @return array $customTokens - * return custom field tokens in array('custom_N' => 'label') format - */ - public function getCustomTokens($entity) { - $customTokens = array(); - foreach (\CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) { - $customTokens["custom_$id"] = $info['label']; - } - - return $customTokens; - } - /** * Alter the query which prepopulates mailing data * for scheduled reminders. -- 2.25.1