2

I'm trying to privide a token for mailing related automated messages. I followed the instructions about hook_civicrm_tokens and hook_civicrm_tokenValues but it does only work for "CiviMail" mails. In mails for group subscriptions (at least "Subscribe Message", "Resubscribe Message", "Unsubscribe Message" and "Welcome Message") the custom token will not be replaced.

function mysite_civicrm_tokens(&$tokens){
  // Define tokens for date (like today's year)
  $tokens['date'] = [
    'date.year' => ts("Today's Date: year")
  ];
}

 function mysite_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = [], $context = null) {
  // Set tokens values for date
  if(isset($tokens['date'])) {
    // TODO: Check if token is required.
    foreach ($cids as $cid) {
      $values[$cid]['date.year'] = date("Y");
    }
  }
}

The custom token defined in mysite_civicrm_tokens is not availible in mysite_civicrm_tokenValues although I have placed it inside the mailing components under civicrm/admin/component.

I know that those mails are handled different from CiviMail mails so I guess there might be another way to solve this.

1 Answer 1

1

Those are Smarty templates, and don't use regular CiviCRM tokens - which is confusing because the tokens can look the same.

Fortunately, Smarty is a full-fledged templating language. You can do date formatting pretty easily. I think you want this:

{$smarty.now|date_format:"%Y"}
1
  • Unfortunately this does not work for me. Smarty seems not to be interpreted either, although I have enabled it in civicrm.settings.php with define( 'CIVICRM_MAIL_SMARTY', 1 ); and clearing cache afterwards. Are you sure this works within the "Subscribe Message"? Commented Oct 23, 2020 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.