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) »
  • hook for tokens works everywhere EXCEPT civimail
Pages: [1]

Author Topic: hook for tokens works everywhere EXCEPT civimail  (Read 2426 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 05:15:31 am
I have a hook that provides a custom token for today's date, using the code at:
http://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit

The function in my code is:
function hook_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  // Date tokens
  if (!empty($tokens['date'])) {
    $date = array(
      'date.date_short' => date('m/d/Y'),
      'date.date_med' => date('M j Y'),
      'date.date_long' => date('F jS, Y'),
    );
    foreach ($cids as $cid) {
      $values[$cid] = empty($values[$cid]) ? $date : $values[$cid] + $date;
    }
  }
}

This code works in for creating PDF letters, and the "send mail" function.    However, if I use it within CiviMail to do a mass mailing, the token is empty.   ( The preview step, and the send test message within the CiviMail wizard  work as expected.)

I am using version 4.2.7.

Any ideas on what to investigate?
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 06:14:13 am
Are you using it in a drupal module or native extension?

Are you sending the cron on cli or wget url?

On 4.2.7, some drupal modules weren't loaded properly when calling from the cli, it could explain.

If you can, try to put the hooks into a native extension, should solve the problem
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 06:19:29 am
This is a native Drupal module.   the CiviCRM cron is called via WGET
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 01:22:15 pm
I have repackaged the token hook into a new extension.  However, I am having the same issue as before.

One thing I forgot to mention:  If I click "Administer ... System Settings ... Scheduled Jobs" and trigger the mass mailing job by clicking "execute now" the civimail message works as expected. (The tokens get filled in).   But if the scheduled job is triggered from "WGET" then the tokens are empty.
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 02:12:55 pm
I found a work-around ( or long-term solution maybe?).

I edited the file "<drupal home>/sites/all/modules/civicrm/bin/cron.php"

to make sure Drupal is bootstrapped before CiviCRM is.

 
Code: [Select]
chdir ( $client_home_dir.'/public_html'  ) ;
define('DRUPAL_ROOT', $client_home_dir.'/public_html');
require_once ( DRUPAL_ROOT.'/includes/bootstrap.inc' );
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);




$config = CRM_Core_Config::singleton();
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 02:50:00 pm
Quote from: epg on June 05, 2013, 01:22:15 pm
I have repackaged the token hook into a new extension. 

You mean a native civicrm extension or a drupal module? normally, you don't need the bootstrap to get the native extensions (and their hooks) working properly

Anyway, seems you have found the solution by bootstraping drupal.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
June 05, 2013, 08:26:34 pm
Xavier - I did try your suggestion by packaging my code as a CiviCRM native extension. However, the extension did not work when I used the CiviCRM-provided "cron.php" to run the scheduled mail job.
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: hook for tokens works everywhere EXCEPT civimail
June 06, 2013, 01:13:06 pm
There is a setting in civicrm.settings.php that controls whether smarty is on for civimail - have you seen that?
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

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
June 07, 2013, 12:58:07 pm
I didn't think that setting was relevant for my situation because I am not using SMARTY in the message templates. The tokens are implemented using the token hooks. 
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
January 09, 2014, 07:13:32 am
Quote from: epg on June 05, 2013, 02:12:55 pm
I found a work-around ( or long-term solution maybe?).

I edited the file "<drupal home>/sites/all/modules/civicrm/bin/cron.php"

to make sure Drupal is bootstrapped before CiviCRM is.

 
Code: [Select]
chdir ( $client_home_dir.'/public_html'  ) ;
define('DRUPAL_ROOT', $client_home_dir.'/public_html');
require_once ( DRUPAL_ROOT.'/includes/bootstrap.inc' );
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);




$config = CRM_Core_Config::singleton();

Hey epg, is this your final solution or did you solved your issue with another one?

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: hook for tokens works everywhere EXCEPT civimail
January 17, 2014, 08:36:37 pm
This is my final solution. I did not find a better work-around
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • hook for tokens works everywhere EXCEPT civimail

This forum was archived on 2017-11-26.