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) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • Custom Tokens in CiviMail
Pages: [1]

Author Topic: Custom Tokens in CiviMail  (Read 668 times)

friendswoodtech

  • I’m new here
  • *
  • Posts: 28
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1.56
  • PHP version: 5.3.6
Custom Tokens in CiviMail
November 20, 2014, 01:13:35 pm
I think I have read all of the forum posts on this issue and I have scoured the internet looking for similar issues.  I have implemented Coleman's token replacement in Civimail 4.5.4, running Drupal 7.34 in a drupal module.

In this module, I have implemented a new custom token for date values, and it works beautifully in Civimail.  But, I'm having issues with the token value replacement for first name & nickname. 

This token gets replaced properly in single emails (with no first name or nick name, both tokens will get replaced with Friend), but not in CiviMail bulk mail.  I have tried inserting {contact.first_name} and {contact.nick_name} and neither work.  If my contact has a nick name, it is replaced properly and if they have a first name, it is replaced properly, but if there is no nick name or first name, then the token is replaced with a blank.

Below is the module.   

Code: [Select]
<?php

function civitokens_civicrm_tokens(&$tokens) {
  
$tokens['date'] = array(
    
'date.date_short' => 'Today\'s Date: mm/dd/yyyy',
    
'date.date_med' => 'Today\'s Date: Mon d, yyyy',
    
'date.date_long' => 'Today\'s Date: Month dth, yyyy',
  );
}

function 
civitokens_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  
$contacts = implode(',', $cids);
  
$tokens += array(
    
'contact' => array(),
  );

  
// Fill first name and nick name with default values
  
if (in_array('first_name', $tokens['contact']) || in_array('nick_name', $tokens['contact'])) {
    
$dao = &CRM_Core_DAO::executeQuery("
      SELECT first_name, nick_name, contact_type, id
      FROM civicrm_contact
      WHERE id IN (
$contacts)"
    
);
    while (
$dao->fetch()) {
      
$cid = $dao->id;
      if (!(
$values[$cid]['first_name'] = $dao->first_name)) {
        
$values[$cid]['first_name'] = $dao->contact_type == 'Individual' ? 'Friend' : 'Friends';
      }
      if (empty(
$values[$cid]['nick_name']) || $dao->contact_type != 'Individual') {
        
$values[$cid]['nick_name'] = $values[$cid]['first_name'];
      }
    }
  }

  
// 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;
    }
  }
}

Any assistance or suggestions anyone can provide would be greatly appreciated.

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Custom Tokens in CiviMail
November 24, 2014, 01:03:41 am
Tricky.  Not sure what the underlying cause is, but it sounds like it is either

* core tokens are not being overridden in CiviMail
* something in your code is not quite write

One thing I would be tempted to try is see if the token replacement works when you are defining a new token, i.e. {contact_test.first_name} and {contact_test.nick_name}.

You would have to define these in the initial civitokens_civicrm_tokens etc. before using them, but it would help narrow down and possibly circumvent the problem.
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

friendswoodtech

  • I’m new here
  • *
  • Posts: 28
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1.56
  • PHP version: 5.3.6
Re: Custom Tokens in CiviMail
February 04, 2015, 05:05:53 pm
Finally getting back to trying to solve this issue. 

The issue revolves around this line:
Code: [Select]
if (in_array('first_name', $tokens['contact']) || in_array('nick_name', $tokens['contact']))
The token used in my template is {contact.first_name}.  If I send an email to a contact using the template, the token replacement works.  But if I use Civimail, it does not.  By commenting out the code above where it checks to see if the first_name or nick_name tokens are used, my token replacement works in Civimail too.

I'll attempt to dig into Civimail to see if I can figure out why.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • Custom Tokens in CiviMail

This forum was archived on 2017-11-26.