Answers.Body: <p>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.  </p>
  
  <p>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)...</p>
  
  <p><a href="https://i.sstatic.net/yUyCt.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yUyCt.jpg" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-30T14:44:19.637Z
Answers.Text: Nice!  Is that code available publicly? A link would be good to help others facing a similar problem.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-31T00:11:14.783Z
Answers.Text: @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).
  
  https://spectrumz.com/civicrm-token-to-list-and-total-donation-from-the-previous-calendar-year-spouses-donations-included/
Answers.UserDisplayName: Don Alejandro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-31T10:00:52.670Z
Answers.Text: 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.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-02T13:57:20.697Z
Answers.Text: 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).
Answers.UserDisplayName: Don Alejandro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T23:51:31.843Z
Answers.LastActivityDate: 2020-01-29T23:51:31.843Z
Answers.UserDisplayName: Don Alejandro
Body: <p>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:</p>
  
  <p>Donation Amount $ 5.00, 10.00</p>
  
  <p>Date of Donation September 14th, 2019, September 12th, 2019</p>
  
  <p>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:  </p>
  
  <pre>$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
  )
  )
  </pre>
  
  <p>Is there any way to access/modify contribution tokens from within hook_civicrm_tokenValues?</p>
  
Tags: <contributions><tokens>
Title: Is it possible to access contribution tokens in hook_civicrm_tokenValues?
ThreadText: # Is it possible to access contribution tokens in hook_civicrm_tokenValues?
  
  (Asked by: username_0 on 2020-01-27)
  
  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?
  
  **Tags:** contributions, tokens
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-01-29. Score: 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](https://i.sstatic.net/yUyCt.jpg)](https://i.sstatic.net/yUyCt.jpg)
  
  > **Comments:**
  > **username_1**: Nice!  Is that code available publicly? A link would be good to help others facing a similar problem.  
  > **username_0**: @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).  https://spectrumz.com/civicrm-token-to-list-and-total-donation-from-the-previous-calendar-year-spouses-donations-included/  
  > **username_1**: 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.  
  > **username_0**: Thanks @username_1 – 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).  
  
  ---
  
  