Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate Template Params for Offline Event Confirmation Receipts #22878

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions CRM/Event/Form/Participant.php
Expand Up @@ -1517,12 +1517,23 @@ public function submit($params) {
$this->assign('amount', $eventAmount);
}

// Assemble event info to send to the message template
$eventDetails = [];
$eventParams = ['id' => $params['event_id']];
CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails);
CRM_Event_BAO_Event::setOutputTimeZone($eventDetails);

$tplParams = [
'event' => $eventDetails,
];

$sendTemplateParams = [
'groupName' => 'msg_tpl_workflow_event',
'valueName' => 'event_offline_receipt',
'contactId' => $contactID,
'isTest' => !empty($this->_defaultValues['is_test']),
'PDFFilename' => ts('confirmation') . '.pdf',
'tplParams' => $tplParams,
Copy link
Member

Choose a reason for hiding this comment

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

@alifrumin Here's a fun bit since v5.42 - the sendTemplate() function accepts an argument tokenContext, as in:

$sendTemplateParams = [
  ...
  'tokenContext' => ['eventId' => 123, 'participantId' => 456],
];

The upshot of using tokenContext is:

  • It enables Civi-style tokens (eg {event.description} or {participant.status_id:label}) which match tokens used elsewhere (eg Scheduled Reminders).
  • It doesn't need CRM_Event_BAO_Event::retrieve -- it will autoload data that is actually used.
    • (Ex: If the template says Go to {event.title} at {event.location}, then it effectively runs SELECT title, location FROM civicrm_event WHERE id = $eventId)

(There's another possible level to this - where we declare that event_offline_receipt expects parameters X,Y,Z. That makes it easier to provide admin tools - eg token-pickers and previews. I don't know if that's in-scope for what you're trying to address.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll let @alifrumin confirm but I did want to comment on this that I think it's not about the message template it's about alterMailParams. This is similar to the earlier ticket regarding contact id. In hook_alterMailParams, you don't have access to anything except what is passed to Mail::send(), which is really only guaranteed to be the message text and the recipient email address (which might match on several contacts so isn't an identifier), and you don't have tokenContext available even when it's from a message template (unless I'm missing how to get at it).

But altermailparams can be called from so many different paths, not just message templates, and so what variables make sense at any given time is going to be different (it may not even have a related contact id). So it would be nice to handle in a clean and documented/unit-tested way.

Copy link
Member

Choose a reason for hiding this comment

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

Right, the list of fields (contactId vs contactId+eventId vs tplParams['event']['title']) represents a contract that should (ideally) be documented. That contract impacts a few different parties - eg (a) the code that fires event_offline_receipt, (b) the admin editing event_offline_receipt, (c) any extensions that hook into event_offline_receipt, and (d) any tests or previews involving event_offline_receipt.

If one just wants to make the {event.*} tokens functional, then it's not strictly necessary to define the contract. You just need to pass along $sendTemplateParams['tokenContext']['eventId'].

But if we're going to document the contract and use it for hooks, then I'd suggest two more pieces:

];

// try to send emails only if email id is present
Expand Down