From 590111eff9de7c43cb75af49252042a541982585 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Mon, 24 Feb 2020 15:30:16 +0000 Subject: [PATCH] Fix values passed to tokenValues hook --- CRM/Contact/Form/Task/Label.php | 1 + CRM/Contact/Form/Task/LabelCommon.php | 1 + CRM/Mailing/BAO/Mailing.php | 12 ++++++------ CRM/Mailing/Page/Preview.php | 2 +- CRM/Utils/Hook.php | 2 +- CRM/Utils/Token.php | 8 +++----- Civi/Token/TokenCompatSubscriber.php | 7 ++----- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/CRM/Contact/Form/Task/Label.php b/CRM/Contact/Form/Task/Label.php index 02e73d127d..92252cad4b 100644 --- a/CRM/Contact/Form/Task/Label.php +++ b/CRM/Contact/Form/Task/Label.php @@ -204,6 +204,7 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts, TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, $primaryLocationOnly); $messageToken = CRM_Utils_Token::getTokens($mailingFormat); + // $details[0] is an array of [ contactID => contactDetails ] // also get all token values CRM_Utils_Hook::tokenValues($details[0], $this->_contactIds, diff --git a/CRM/Contact/Form/Task/LabelCommon.php b/CRM/Contact/Form/Task/LabelCommon.php index ddc289154d..c0c561ff26 100644 --- a/CRM/Contact/Form/Task/LabelCommon.php +++ b/CRM/Contact/Form/Task/LabelCommon.php @@ -140,6 +140,7 @@ class CRM_Contact_Form_Task_LabelCommon { $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $messageToken = CRM_Utils_Token::getTokens($mailingFormat); + // $details[0] is an array of [ contactID => contactDetails ] $details = $details[0]; $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details); diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 4951fe4bea..a2b20bcf32 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1080,15 +1080,18 @@ ORDER BY civicrm_email.is_bulkmail DESC elseif ($contactId === 0) { //anonymous user $contact = []; - CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); + CRM_Utils_Hook::tokenValues($contact, [$contactId], $job_id); } else { $params = [['contact_id', '=', $contactId, 0, 0]]; list($contact) = CRM_Contact_BAO_Query::apiQuery($params); + // $contact is an array of [ contactID => contactDetails ] - //CRM-4524 - $contact = reset($contact); + // also call the hook to get contact details + CRM_Utils_Hook::tokenValues($contact, [$contactId], $job_id); + // Don't send if contact doesn't exist + $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1', [1 => $contactId] @@ -1098,9 +1101,6 @@ ORDER BY civicrm_email.is_bulkmail DESC $res = NULL; return $res; } - - // also call the hook to get contact details - CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id); } $pTemplates = $this->getPreparedTemplates(); diff --git a/CRM/Mailing/Page/Preview.php b/CRM/Mailing/Page/Preview.php index 97a15798c4..4d7981481d 100644 --- a/CRM/Mailing/Page/Preview.php +++ b/CRM/Mailing/Page/Preview.php @@ -66,7 +66,7 @@ class CRM_Mailing_Page_Preview extends CRM_Core_Page { $mailing->getFlattenedTokens(), get_class($this) ); - + // $details[0] is an array of [ contactID => contactDetails ] $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail, TRUE, $details[0][$session->get('userID')], $attachments ); diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 0d09ab914b..87c4e98c7f 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -884,7 +884,7 @@ abstract class CRM_Utils_Hook { * tokens returned by the 'tokens' hook * * @param array $details - * The array to store the token values indexed by contactIDs (unless it a single). + * The array to store the token values indexed by contactIDs. * @param array $contactIDs * An array of contactIDs. * @param int $jobID diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 6dd024553f..afb69333b6 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1228,7 +1228,6 @@ class CRM_Utils_Token { } $details = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs), TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, TRUE); - $contactDetails = &$details[0]; foreach ($contactIDs as $contactID) { @@ -1263,8 +1262,9 @@ class CRM_Utils_Token { } } + // $contactDetails = &$details[0] = is an array of [ contactID => contactDetails ] // also call a hook and get token details - CRM_Utils_Hook::tokenValues($details[0], + CRM_Utils_Hook::tokenValues($contactDetails, $contactIDs, $jobID, $tokens, @@ -1291,9 +1291,7 @@ class CRM_Utils_Token { * @return array * contactDetails with hooks swapped out */ - public static function getAnonymousTokenDetails($contactIDs = [ - 0, - ], + public static function getAnonymousTokenDetails($contactIDs = [0], $returnProperties = NULL, $skipOnHold = TRUE, $skipDeceased = TRUE, diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 45a81fb174..ae8a55f616 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -89,12 +89,9 @@ class TokenCompatSubscriber implements EventSubscriberInterface { $contact = array_merge($contact, $row->context['tmpTokenParams']); } - $contactArray = !is_array($contactId) ? [$contactId => $contact] : $contact; - - // Note: This is a small contract change from the past; data should be missing - // less randomly. + $contactArray = [$contactId => $contact]; \CRM_Utils_Hook::tokenValues($contactArray, - (array) $contactId, + [$contactId], empty($row->context['mailingJobId']) ? NULL : $row->context['mailingJobId'], $messageTokens, $row->context['controller'] -- 2.25.1