We've written a custom token to pull a piece of custom data off of any open cases. The token works as an imploded variable. I'd like to be able to use the token as an array as well (so I can run a for each loop in an email communication.) I am having trouble accessing the array.

If I use the variable {extrainfo.namearray} the email prints "Array". I've tried a simple foreach loop

{foreach from=extrainfo.namearray item=value key=label}

This prints out an "0" and "extrainfo.namearray" I can only assume that's becuase I don't have the "$" before the token name. The statement prints out nothing if I add the $ to the token name.

I know the array has content, if I use {debug} in an email, I can see that it is part of the {$contact} tokens. It prints our the array itself with the expected data:

extrainfo.namearray => Array (3)
97485 => "Name 1"
97486 => "Name 2"
97547 => "Name 3"

I also tried accessing it in this manner {$contact.extrainfo.namearray} but that prints nothing (not even "Array").

Any advice on how I can target/loop items in the {extrainfo.namearray} array?

  • With some more testing, I've found that while the debug lists the {$contact} array with 23 items and includes my custom extrainfo.namearray (and several other related custom tokens). But when I loop through the {$contact} token in an email it only lists the standard contact tokens - about 8 of them - but does not list any of the custom tokens we've written. Any idea why a debug will list the custom tokens but a basic loop {foreach from=$contact item=value key=label} won't?
    – RayWright
    Commented Dec 7, 2017 at 21:16

2 Answers 2


Through further testing I found that if I FIRST include the {extrainfo.namearray} token (which simply prints 'array') and THEN run a loop through the contact array, it does in fact show all of the tokens!

In the case anyone else finds this useful, my final code:

{extrainfo.namearray} // hide this so not seen in email
  {foreach name=outer from=$contact key=k item=it} 
     {if $k == 'extrainfo.namearray' }
       {foreach name=mid from=$it item=inn} 

I'm not sure why I have to include the custom array first before it will include it in the contact array so that I can properly loop through it - but alas it's a workaround that works!!!


The substitution of the email tokens is done by the CRM_Utils_Token class, not by smarty. But is possible to extend the smarty template variables using the following code.

 $template = CRM_Core_Smarty::singleton();
 $template -> assign('extrainfo',array(
   97485 => "Name 1"
   97486 => "Name 2"
   97547 => "Name 3"

You have however to find the correct hook to add this code. A good candidate is maybe the civicrm_postProcess hook of the form that sends the email.

Another, more generic, possibility to explore is to use {crmApi} smarty tag to use the Api to add more information to an email. Examples are generated by the API explorer.

  • While in this case I'm unable to affect the code that creates these tokens (I did find a work around, posted as a separate answer) are you saying that using the hook_civicrm_tokens is not enough? I should also assign the tokens a template?
    – RayWright
    Commented Dec 18, 2017 at 16:27

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.