From a0fcf9925d8ba218d80d1f4ac80819b267cf7c5f Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Tue, 3 Jan 2017 18:09:45 +0530 Subject: [PATCH] CRM-18141: Scheduled reminders for activities not rendering custom field tokens --- Civi/Token/AbstractTokenSubscriber.php | 39 +++++++++++++++++++++++--- Civi/Token/TokenCompatSubscriber.php | 15 +++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Civi/Token/AbstractTokenSubscriber.php b/Civi/Token/AbstractTokenSubscriber.php index 81cfd91eb473..6cb8609bb445 100644 --- a/Civi/Token/AbstractTokenSubscriber.php +++ b/Civi/Token/AbstractTokenSubscriber.php @@ -148,19 +148,50 @@ public function evaluateTokens(TokenValueEvent $e) { } $activeTokens = array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames)); - if (empty($activeTokens)) { - return; - } + $extraTokens = array_diff($messageTokens[$this->entity], array_keys($this->tokenNames)); $prefetch = $this->prefetch($e); foreach ($e->getRows() as $row) { - foreach ($activeTokens as $field) { + $this->evaluateExtraToken($row, $this->entity, $extraTokens); + foreach ((array) $activeTokens as $field) { $this->evaluateToken($row, $this->entity, $field, $prefetch); } } } + /** + * Populate the custom field and other remaining entity token data. + * + * @param TokenRow $e + * The record for which we want token values. + * @param string $entity + * The entity for which we want the token values + * @param array $tokens + * The array of tokens whose data need to be fetched + */ + public function evaluateExtraToken(TokenRow $row, $entity, $tokens) { + if (empty($tokens)) { + return; + } + + $actionSearchResult = $row->context['actionSearchResult']; + + try { + $result = civicrm_api3($entity, 'getsingle', array( + 'id' => $actionSearchResult->entity_id, + 'return' => $tokens, + )); + } + catch (CiviCRM_API3_Exception $e) { + return; + } + + foreach ($tokens as $token) { + $row->tokens($entity, $token, \CRM_Utils_Array::value($token, $result, '')); + } + } + /** * To perform a bulk lookup before rendering tokens, override this * function and return the prefetched data. diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index e6c2f72e65f2..b03195cd26fb 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -61,6 +61,20 @@ public function onEvaluate(TokenValueEvent $e) { // FIXME: Need to differentiate errors which kill the batch vs the individual row. throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); } + + $contactTokens = \CRM_Utils_Array::value('contact', $messageTokens); + if (!empty($contactTokens)) { + try { + $result = civicrm_api3('Contact', 'getsingle', array( + 'id' => $contactId, + 'return' => $contactTokens, + )); + $contact = array_merge($contact, $result); + } + catch (CiviCRM_API3_Exception $e) { + //do nothing + } + } } else { $contact = $row->context['contact']; @@ -76,7 +90,6 @@ public function onEvaluate(TokenValueEvent $e) { // Note: This is a small contract change from the past; data should be missing // less randomly. - //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']); \CRM_Utils_Hook::tokenValues($contactArray, (array) $contactId, empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id,