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) »
  • How the heck do you debug mail tokens hooks?
Pages: [1]

Author Topic: How the heck do you debug mail tokens hooks?  (Read 1344 times)

questions

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: Durpal, 7
  • MySQL version: 5.1
  • PHP version: 5.3
How the heck do you debug mail tokens hooks?
August 29, 2011, 11:00:32 pm
I spent a lot of time wading through documentation to figure out how to add a token to an email.  I took the example for contribution and changed the query to get info from membership.  Enabled the module.  Added the token between {} into my mailing.  Nothing is replaced in my test emails.  I haven't a clue how to debug this. 

My query works from a sql prompt, well with mods to remove the contact_id in stuff.

Should I see the new token I added when I click on insert token from the New Mailing form?  I'm guessing not.

In the examples the functions are named myhooks_civicrm_tokens and myhooks_civicrm_tokenValues, what calls these?  Are these names baked in so these names get called or do I need to have the names of these functions stuck in some other chunk of code so they get called or does it matter what they named?

I'm running 3.2.4 drupal 6.19

Here is the code I stuck in sites/all/modules/hooks/hooks.module

<?php

# implement the tokens hook so we can add our new token to the list of tokens
# displayed to CiviMail users
function myhooks_civicrm_tokens( &$tokens ) {
    $tokens['affiliation'] =
        array('affiliation.ug' );
}

function myhooks_civicrm_tokenValues( &$details, &$contactIDs ) {
    # prepare the contact ID(s) for use in a database query
    if ( is_array( $contactIDs ) ) {
        $contactIDString = implode( ',', array_values( $contactIDs ) );
        $single = false;
    } else {
        $contactIDString = "( $contactIDs )";
        $single = true;
    }

    # build the database query
    $query = "
      SELECT contact_id,
             concat(mt.name, ' class of ' , date_format(m.end_date,'%y')) as ug
      FROM   civicrm_membership m,
             civicrm_membership_type mt
      WHERE  mt.id= m.membership_type_id
      and    m.contact_id in ( $contactIDString )
      and    curdate() between m.start_date and m.end_date
      and    m.membership_type_id = 1
      and    is_test = 0
    ";

    # run the query
    $dao = CRM_Core_DAO::executeQuery( $query );
    while ( $dao->fetch( ) ) {
        if ( $single ) {
            $value =& $details;
        } else {
            if ( ! array_key_exists( $dao->contact_id, $details ) ) {
                $details[$dao->contact_id] = array( );
            }
            $value =& $details[$dao->contact_id];
        }

        # set the token's value
        $value['affiliation.ug'] = $dao->ug;
    }
}

questions

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: Durpal, 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: How the heck do you debug mail tokens hooks?
August 30, 2011, 10:19:44 am
I think my problems are more basic.  I tried replacing my code with the example code for contribution.largest.  I cleared the cache.  I rebuilt the modules (ran update.php via the admin page which goes to Drupal database update).  Still the mail token is blank on the preview mailing.  What am I missing?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: How the heck do you debug mail tokens hooks?
August 30, 2011, 11:31:42 am
It looks like your hook isn't being called. (I suppose the module is enabled?)

Is your hook named correctly? What is your actual module name / actual hook name (presumably myhooks is just a placeholder)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

questions

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: Durpal, 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: How the heck do you debug mail tokens hooks?
August 30, 2011, 01:58:47 pm
The module is just named hooks.  The files are hooks.info and hooks.module.  I assume it finds at least the .info one since I was able to enable it.   Since I couldn't get mine to work, I figured I'd try the example so I just cut and paste it, so at the moment they are called myhooks_civicrm_tokens and myhooks_civicrm_tokenValues.  I assume the functions just need to end in civicrm_tokens and civicrm_tokenValues.   

Is there someplace else one has to enable something?  Is there a way to see if it's even calling the functions?

questions

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: Durpal, 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: How the heck do you debug mail tokens hooks?
August 30, 2011, 11:09:40 pm
I found my problem.  I suppose in hindsight I should have seen this but none of the documentation that I came across mentions this explicitly.

Apparently, as Eileen eluded, the names of the modules and functions are critical. 

I don't know for sure and don't want to spend the time testing it, but it seems that the function names beginning must match the module name.

So if I name a module acme_hooks and I have the files acme_hooks.info and acme_hooks.module the functions in acme_hooks.module must start with acme_hooks, e.g. acme_hooks_civicrm_token, acme_hooks_civicrm_tokenValues.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How the heck do you debug mail tokens hooks?
August 31, 2011, 12:17:57 am
Quote from: questions on August 30, 2011, 11:09:40 pm
I found my problem.  I suppose in hindsight I should have seen this but none of the documentation that I came across mentions this explicitly.


Could you update the wiki to add this info?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

questions

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: Durpal, 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: How the heck do you debug mail tokens hooks?
August 31, 2011, 09:59:48 pm
I updated the wiki a touch to clarify this.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How the heck do you debug mail tokens hooks?

This forum was archived on 2017-11-26.