Commit 6cab7c2b authored by Rich's avatar Rich

Add tokens for flexmailer/TokenProcessor

parent 0c55241f
......@@ -157,3 +157,76 @@ function actionlinks_civicrm_navigationMenu(&$menu) {
));
_actionlinks_civix_navigationMenu($menu);
}
function actionlinks_civicrm_container($container) {
$container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__));
$container->findDefinition('dispatcher')->addMethodCall('addListener',
array(\Civi\Token\Events::TOKEN_REGISTER, 'actionlinks_register_tokens')
);
$container->findDefinition('dispatcher')->addMethodCall('addListener',
array(\Civi\Token\Events::TOKEN_EVALUATE, 'actionlinks_evaluate_tokens')
);
}
function actionlinks_register_tokens(\Civi\Token\Event\TokenRegisterEvent $e) {
$contact = $e->entity('contact');
// Get list of all active Action Links, add tokens for them like actionLinkN where N is the ID.
$bao = new CRM_Actionlinks_BAO_ActionLink();
$bao->is_active = 1;
if (!$bao->find()) {
return;
}
while ($bao->fetch()) {
$contact->register("actionLink$bao->id", ts('Action Link "%1"', [1 => $bao->name]));
}
}
function actionlinks_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
$tokensInUse = $e->getTokenProcessor()->getMessageTokens();
$actionLinkIDs = [];
foreach ($tokensInUse['contact'] ?? [] as $token) {
$matches = [];
if (preg_match('/^actionLink(\d+)$/', $token, $matches)) {
$actionLinkIDs[] = (int) $matches[1];
}
}
if (!$actionLinkIDs) {
// No actionLink tokens
return;
}
// get contactIDs.
$contactIDs = [];
foreach ($e->getRows() as $row) {
$a = $row->context;
if (((int) ($row->context['contact_id'] ?? 0)) > 0) {
$contactIDs[] = (int) $row->context['contact_id'];
}
}
// Create links
$tokens = [];
foreach ($actionLinkIDs as $actionLinkID) {
$result = \Civi\Api4\ActionLink::getLink()
->setActionLinkID($actionLinkID)
->setContactIDs($contactIDs)
->execute();
foreach ($result as $actionLinkRow) {
$tokens[$actionLinkRow['contactID']][$actionLinkID] = $actionLinkRow['link'];
}
}
// Put tokens in place.
foreach ($e->getRows() as $row) {
/** @var TokenRow $row */
$row->format('text/html');
if (((int) ($row->context['contact_id'] ?? 0)) > 0) {
// We have a contact ID.
foreach ($tokens[$row->context['contact_id']] as $actionLinkID => $v) {
$row->tokens('contact', "actionLink$actionLinkID", $v);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment