Skip to content

Commit

Permalink
CRM-18141: Scheduled reminders for activities not rendering custom fi…
Browse files Browse the repository at this point in the history
…eld tokens
  • Loading branch information
monishdeb committed Jan 12, 2017
1 parent 0a0c2fc commit a0fcf99
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
39 changes: 35 additions & 4 deletions Civi/Token/AbstractTokenSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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,
Expand Down

0 comments on commit a0fcf99

Please sign in to comment.