CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Custom Contribution Token
Pages: [1]

Author Topic: Custom Contribution Token  (Read 917 times)

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Custom Contribution Token
March 19, 2014, 10:32:09 pm
I'm having trouble implementing the following Hook code for a contribution custom field token.  I think the problem is in the contribution IDs because the query seems to work fine directly through phpmyadmin.  Any insight would be greatly appreciated!  Thanks!

function wordpress_civicrm_tokens(&$tokens) {
   $tokens['custom_contrib'] = array(
      'custom_contrib.item_description' => 'Custom Contrib.: Item Description',
   );
}
function wordpress_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
   if (!empty($tokens['custom_contrib'])) {
      $cc_ids = implode(',', $cids);
      $tokens += array(
         'item_description' => array(),
      );         
      if (in_array('item_description', $tokens['custom_contrib'])) {
         $dao = &CRM_Core_DAO::executeQuery("
            SELECT entity_id, donation_description_30 AS item_desc
            FROM civicrm_value_in_kind_donation_info_9
            WHERE entity_id IN ($cc_ids)"
         );
         while ($dao->fetch()) {
            $ccid = $dao->entity_id;
            if (in_array($ccid, $cids)) {
               $values[$ccid]['custom_contrib.item_description'] = $dao->item_desc;
            }
         }      
      }
   }
}

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom Contribution Token
March 20, 2014, 08:30:57 am
Have you used a debugger to see what values are being passed for the variables? $cids might be contact ids not contribution ids.
Try asking your question on the new CiviCRM help site.

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Re: Custom Contribution Token
March 20, 2014, 08:48:50 am
I'm pretty sure I'm in the "contribution mode" (ie., Selected "Thank You Letters for Contributions" after "Find Contributions") so I assume the $cids are contribution IDs.  Honestly, I have not figured out the debugger and am fairly new to php. 
P.S. You probably recognize this code as rip-offs from your very own "Create your own Tokens for Fun and Profit"!

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom Contribution Token
March 20, 2014, 08:55:40 am
Sorry debugging variables is not optional in any programming language - there's just absolutely no way to write code without having any idea what a variable contains. And you know what "assume" makes an ass out of... ;)
Just put this at the beginning of your hook tokenValues:
echo "<pre>";
echo "\n\$cid = ";
print_r($cid);
echo "\n\$values = ";
print_r($values);
echo "</pre>";
exit();
Try asking your question on the new CiviCRM help site.

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Re: Custom Contribution Token
March 20, 2014, 09:11:20 am
Thanks for highlighting the debugging path for me!
This sounds stupid, but where is this output captured?  Nothing comes to the screen, pdf or log file.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom Contribution Token
March 20, 2014, 09:18:47 am
Echo and print go directly to the browser. If you don't see a white-screen with that stuff on it in your browser, then your hook is not even running.
I 'assume' you are browsing to the right place to make that code go (e.g. actually printing a contribution letter).
If that's true and it's still not running, try putting a print/exit statement in the other hook to see if that works.

One more thing and this may be completely obvious but CIviCRM already has tokens for contribution amount - have you tried using that?
Try asking your question on the new CiviCRM help site.

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Re: Custom Contribution Token
March 20, 2014, 09:38:26 am
I click the "Make Thank You Letters" Button and a new pdf is downloaded.
I'm not seeing this magical white screen (I have echo/print statements in both hooks) so no hook explains why no item description (this is a text field describing an in-kind donation so I can't use the contribution amount).
At least I can bang my head against the wall about something else now! Thanks, Karen

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Re: Custom Contribution Token
March 20, 2014, 10:08:17 am
SORRY - put debugging print in wrong civicrmhooks.php file.  Now getting debugging print! (had to put $cids not $cid though)
And it is the contact id rather that contribution id!!  How can I tell it I need the contribution IDs in the $cids?

snoopers01

  • I’m new here
  • *
  • Posts: 18
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5
  • MySQL version: 5.5.24
  • PHP version: 5.4.3
Re: Custom Contribution Token
March 20, 2014, 03:58:24 pm
In CRM/Utils/Token.php there is a comment around line 1148 in the static function getContributionTokenDetails in a loop on contribution IDs to:
        // TODO: call a hook to get token contribution details
Any call/update here would not be upgrade proof.
Is the feature I'm searching for, Custom Contribution Tokens which key off contribution IDs, not available yet?

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom Contribution Token
March 24, 2014, 04:56:56 pm
Seems likely.
You could fake it by looking up the latest contribution for a given contact. That will probably be what you want most of the time.
Try asking your question on the new CiviCRM help site.

jaapjansma

  • I post frequently
  • ***
  • Posts: 247
  • Karma: 9
    • CiviCoop
  • CiviCRM version: 4.4.2
  • CMS version: Drupal 7
  • MySQL version: 5
  • PHP version: 5.4
Re: Custom Contribution Token
March 26, 2014, 07:18:10 am
I have created some tokens for a client of us in an extension. Which retrieves the last completed contribution and the next pending contribution. See for this extension https://github.com/CiviCooP/org.civicoop.no.maf.ext/blob/master/org.civicoop.no.maf.tokens/maf_tokens.php

This is a custom extension for a client of us. But feel free to use the code and tweak it.
Developer at Edeveloper / CiviCoop

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Custom Contribution Token

This forum was archived on 2017-11-26.