Skip to content

Instantly share code, notes, and snippets.

@totten
Last active March 3, 2022 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totten/14178dabea79ea2a5a639a5b928262e2 to your computer and use it in GitHub Desktop.
Save totten/14178dabea79ea2a5a639a5b928262e2 to your computer and use it in GitHub Desktop.
hook_alterWorkflowMessage - Similar to hook_alterMailParams, but the message is represented as an object
diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php
index ab0f8e7b6e..8357047a1b 100644
--- a/CRM/Core/BAO/MessageTemplate.php
+++ b/CRM/Core/BAO/MessageTemplate.php
@@ -351,10 +351,11 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate implemen
// Allow WorkflowMessage to run any filters/mappings/cleanups.
$model = $params['model'] ?? WorkflowMessage::create($params['workflow'] ?? 'UNKNOWN');
- $params = WorkflowMessage::exportAll(WorkflowMessage::importAll($model, $params));
+ WorkflowMessage::importAll($model, $params);
+ CRM_Utils_Hook::alterWorkflowMessage($params['workflow'] ?? 'UNKNOWN', $model);
+ $params = WorkflowMessage::exportAll($model);
unset($params['model']);
// Subsequent hooks use $params. Retaining the $params['model'] might be nice - but don't do it unless you figure out how to ensure data-consistency (eg $params['tplParams'] <=> $params['model']).
- // If you want to expose the model via hook, consider interjecting a new Hook::alterWorkflowMessage($model) between `importAll()` and `exportAll()`.
$sync();
$params = array_merge($modelDefaults, $viewDefaults, $envelopeDefaults, $params);
diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php
index a9679480a0..95395bb2ad 100644
--- a/CRM/Utils/Hook.php
+++ b/CRM/Utils/Hook.php
@@ -1219,6 +1219,21 @@ abstract class CRM_Utils_Hook {
);
}
+ /**
+ * This is hook is called when an automated workflow message is being composed.
+ *
+ * @param string $name
+ * @param \Civi\WorkflowMessage\WorkflowMessageInterface $model
+ *
+ * @return mixed
+ */
+ public static function alterWorkflowMessage(string $name, \Civi\WorkflowMessage\WorkflowMessageInterface $model) {
+ return self::singleton()->invoke(['name', 'model'], $name, $model,
+ self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject,
+ 'civicrm_alterWorkflowMessage'
+ );
+ }
+
/**
* This hook is called when loading a mail-store (e.g. IMAP, POP3, or Maildir).
*
diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php
index a6e6d555af..1b8a03d044 100644
--- a/Civi/Core/Container.php
+++ b/Civi/Core/Container.php
@@ -443,6 +443,7 @@ class Container {
'CRM_Core_LegacyErrorHandler',
'handleException',
], -200);
+ $dispatcher->addListener('hook_civicrm_alterWorkflowMessage', $aliasEvent('hook_civicrm_alterWorkflowMessage', 'name'), 100);
$dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Activity_ActionMapping', 'onRegisterActionMappings']);
$dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Contact_ActionMapping', 'onRegisterActionMappings']);
$dispatcher->addListener('civi.actionSchedule.getMappings', ['CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings']);

Example

function mymodule_civicrm_alterWorkflowMessage(string $name, $model) {
  // Whenever there's an offline event registration, send BCC email. Get email addr from a custom-field.
  if ($name === 'event_offline_receipt') {
    /** @var \CRM_Event_WorkflowMessage_OfflineReceipt $model */
    $customNotificationList = CustomValue::get('MyEventNotification')
      ->addWhere('entity_id', '=', $model->getEventId())
      ->addSelect('email')
      ->execute();
    foreach ($customNotificationList as $customNotification) {
      $model->addBcc(['email' => $customNotification['email']]);
    }
  }
}

Question

Are there any examples of things in $params that should be accessible in $model but are not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment