From 9cb7f52255ddd29541e07317b2c57e67a2ce625b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 12 Oct 2021 20:02:51 -0700 Subject: [PATCH] (REF) Tokens - Populate "Event"/"Participant" without direct access to actionSearchResult Overview -------- When sending a scheduled remidner for a `civicrm_participant` record, it supports tokens for both `{participant.*}` (`$context['participantId']`) and `{event.*}` (`$context['eventId']`). This creates a special requirement to load the `eventId` in addition to the `participantId`. This patch changes the way in which `eventId` is loaded. Before ------ In `ParticipantToken`, the token-evaluation-step for `{participant.*}` tokens has a side-effect of copying `$context['eventId']` based on reading `actionSearchResult` (although this appears indirect). After ----- The action-search returns `tokenContext_eventId`, which automatically maps to `$context['eventId']`. This means that the `eventId` will be set before evaluation begins. --- CRM/Event/ParticipantTokens.php | 11 ++++++++--- CRM/Event/Tokens.php | 3 --- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CRM/Event/ParticipantTokens.php b/CRM/Event/ParticipantTokens.php index 01d57750ab..9863bf6566 100644 --- a/CRM/Event/ParticipantTokens.php +++ b/CRM/Event/ParticipantTokens.php @@ -74,15 +74,20 @@ class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens { ]; } + public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void { + // When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`. + parent::alterActionScheduleQuery($e); + if ($e->mapping->getEntity() === $this->getExtendableTableName()) { + $e->query->select('e.event_id AS tokenContext_eventId'); + } + } + /** * @inheritDoc * @throws \CiviCRM_API3_Exception */ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { $this->prefetch = (array) $prefetch; - if (empty($row->context['eventId'])) { - $row->context['eventId'] = $this->getFieldValue($row, 'event_id'); - } if ($field === 'balance') { // @todo - is this really a good idea to call this & potentially get the // balance of the contribution attached to 'registered_by_id' diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index 8c437057b1..ca6fb553dd 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -92,9 +92,6 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens { */ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { $eventID = $this->getFieldValue($row, 'id'); - if (!$eventID) { - $eventID = $row->context['actionSearchResult']->event_id; - } if (array_key_exists($field, $this->getEventTokenValues($eventID))) { foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) { $row->format($format)->tokens($entity, $field, $value ?? ''); -- 2.25.1