Skip to content

Commit

Permalink
Reformat message tokens. This did not address the issue at hand, but …
Browse files Browse the repository at this point in the history
…stashing the approach for later.
  • Loading branch information
xurizaemon committed Dec 13, 2016
1 parent 5a7f2fe commit 9053923
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Civi/Token/TokenCompatSubscriber.php
Expand Up @@ -73,6 +73,9 @@ public function onEvaluate(TokenValueEvent $e) {

$contactArray = !is_array($contactId) ? array($contactId => $contact) : $contact;

// Reformat $messageTokens for consistency.
$messageTokens = $this->formatMessageTokens($messageTokens);

// Note: This is a small contract change from the past; data should be missing
// less randomly.
//\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']);
Expand All @@ -91,6 +94,40 @@ public function onEvaluate(TokenValueEvent $e) {
}
}

/**
* Reformat $messageTokens if token names are values.
*
* Some components (@TODO: name names?) pass in $messageTokens in
* TokenProcessor format (token name is value), while others pass in hook
* format (token name is key).
*
* This method reformats $messageTokens if it detects that the token names are
* passed as values.
*
* @param array $messageTokens Per TokenProcessor format
* [ 'contact' => [ 'checksum', 'contact_id' ] ]
* @return array Per hook format
* [ 'contact' => [ 'first_name' => 1, 'email_greeting' => 1 ] ] ]
*/
public function formatMessageTokens($messageTokens) {
$result = [];
// Don't reformat if any entity.token has token names as keys.
foreach ($messageTokens as $entity => $names) {
foreach ($names as $k => $v) {
if (!is_integer($k)) {
return $messageTokens;
}
}
}
// All entity.token names are as values here, so reformat.
foreach ($messageTokens as $entity => $names) {
foreach ($names as $name) {
$result[$entity][$name] = 1;
}
}
return $result;
}

/**
* Apply the various CRM_Utils_Token helpers.
*
Expand Down

2 comments on commit 9053923

@MegaphoneJon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xurizaemon This saved my bacon today, thank you!

@xurizaemon
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! I have no recollection of doing this at all.

Which is why I'm heavy Git user I guess, cos who remembers this stuff?

Please sign in to comment.