Skip to content

Commit

Permalink
Merge pull request #24315 from eileenmcnaughton/cont_tokens_less
Browse files Browse the repository at this point in the history
Fix `returnProperties` on contact retrieval in `Contact_Tokens` to fetch less, less often
  • Loading branch information
seamuslee001 authored Aug 22, 2022
2 parents 1d4e70a + fd6a43f commit c9fafc9
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions CRM/Contact/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,13 @@ public function onEvaluate(TokenValueEvent $e) {
*
* @param \Civi\Token\TokenRow $row
* @param string $field
*
* @return string|int
* @throws \CRM_Core_Exception
*/
protected function getFieldValue(TokenRow $row, string $field) {
$entityName = 'contact';
$contact = $row->context['contact'];
if (isset($this->getDeprecatedTokens()[$field])) {
// Check the non-deprecated location first, fall back to deprecated
// this is important for the greetings because - they are weird in the query object.
Expand All @@ -347,13 +350,23 @@ protected function getFieldValue(TokenRow $row, string $field) {
}

foreach ($possibilities as $possibility) {
if (isset($row->context[$entityName][$possibility])) {
return $row->context[$entityName][$possibility];
if (isset($contact[$possibility])) {
return $contact[$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 (array_key_exists($split[0], $contact)) {
return $row->context['contact'][$possibility] = $this->getPseudoValue($split[0], $split[1], $contact[$split[0]]);
}
}
}

$contactID = $this->getFieldValue($row, 'id');
if ($contactID) {
$row->context['contact'] = array_merge($this->getContact($contactID, $this->activeTokens), $row->context['contact']);
$missingFields = array_diff_key(array_fill_keys($this->activeTokens, TRUE), $contact);
$row->context['contact'] = array_merge($this->getContact($contactID, array_keys($missingFields)), $contact);
if (isset($row->context[$entityName][$field])) {
return $row->context[$entityName][$field];
}
Expand Down

0 comments on commit c9fafc9

Please sign in to comment.