0

I’m trying to make a custom token to present grouped donations in a thank you letter in a friendlier format. If I use the ‘Group contribution by contact’ option the {contribution.total_amount} and {contribution.receive_date} tokens generate something like this:

Donation Amount $ 5.00, 10.00

Date of Donation September 14th, 2019, September 12th, 2019

I’ve setup hook functions to define a new token meant to display each donation and date on the same line, and to provide a total at the end of the list. If I could access the donation tokens in my function for hook_civicrm_tokenValues I could easily re-arrange the data as desired. But even though the $tokens parameter indicates that my template uses {contribution.total_amount} and {contribution.receive_date}, the $values parameter does not include the relevant contribution tokens, just contact tokens:

$values (as displayed by a temp debug token)
Array
(
[2] => Array
(
[contact_id] => 2
[display_name] => Don Alexander
[postal_greeting_id] => 1
[postal_greeting_display] => Dear Don
[postal_greeting_custom] =>
[address_id] => 1891
[street_address] => 1069 Steele Hollow Rd
[supplemental_address_1] =>
[city] => Spencer
[postal_code_suffix] => 8133
[postal_code] => 25276
[state_province_id] => 1047
[civicrm_value_spouse_tokens_7_id] => 1
[custom_16] => Mary
[custom_17] => Don Alexander, Mary Wildfire
[postal_greeting] => Dear Don
[state_province_name] => West Virginia
)
)

Is there any way to access/modify contribution tokens from within hook_civicrm_tokenValues?

2

I think I can self-answer this after exploring various methods to create end of the year thank-you letters for recurring/multiple contributions - the donation tokens are not passed in the $values array, and are not otherwise easily accessible from within the hook function.

In theory it may be possible to access an array of contribution data passed in the Smarty variables, but it was way easier to simply make a custom token that does it's own queries to create a small table showing all donations made in the previous calendar year (including donations made by a spouse)...

enter image description here

4
  • Nice! Is that code available publicly? A link would be good to help others facing a similar problem.
    – Aidan
    Jan 30 '20 at 14:44
  • @aidan I just put a zip file of the token code on my hobby website (not sure yet how to get code to display correctly in a copy-n-paste format). spectrumz.com/… Jan 31 '20 at 0:11
  • That looks good. A few suggestions: 1) putting this in the theme works, but a more standard approach would be to put it in an extension. The 'civix' command will do most of the extension work for you. 2) Using the api is preferred over SQL 3) Most systems operate with a single currency, but ideally you would take the currency from the contribution and only sum those of the same currency - ie avoid adding $ amounts to £ amounts.
    – Aidan
    Jan 31 '20 at 10:00
  • Thanks @Aidan – I haven’t learned how to use Civix or the api yet, but may consider making this into a more generic extension when I’m done moving OVEC off SALSA. fwiw, I do wonder if the SQL to correctly find spouse donations becomes somewhat more difficult using the api – for example, the civitoken extension only finds spouses (or whatever) for contact_id_a of the relationship record (requesting Don’s spouse returns Mary, but requesting Mary’s spouse returns null). Feb 2 '20 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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