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) »
  • Need A Developer for a Custom Token
Pages: [1]

Author Topic: Need A Developer for a Custom Token  (Read 945 times)

John Richardson

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 0
    • Life Challenge of Southeastern Michigan
  • CiviCRM version: 4.5.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.27
  • PHP version: 5.3.13
Need A Developer for a Custom Token
January 17, 2013, 09:51:56 am
To develop a custom token:
Quote
Custom tokens
It is also possible to have custom tokens created by a developer. For instance, the total amount of contributions from a contact. To find out more about working with custom tokens, refer to the discussion about custom mail merge tokens in the Hooks chapter of the Extending CiviCRM section of this book and look at the wiki:http://wiki.civicrm.org/confluence/display/CRMDOC/Tokens.

Another task for a developer is to create if/then logic for your mail merge. This is done using the smarty template language as described herehttp://www.smarty.net/docs/en/language.function.if.tpl.

The quoted text can be found in the documentation here:  http://book.civicrm.org/user/current/common-workflows/tokens-and-mail-merge/

What we need is exactly the token in the second sentence of the quoted text, i.e. "For instance, the total amount of contributions from a contact. "

Has someone already developed such a custom token? If not, does someone know how to do this? Please contact me asap. We need this for my organization desperately.
John Richardson

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Need A Developer for a Custom Token
February 11, 2013, 04:49:49 pm
Quote
Has someone already developed such a custom token?
Yes, and I even blogged about it :)
http://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit
Try asking your question on the new CiviCRM help site.

John Richardson

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 0
    • Life Challenge of Southeastern Michigan
  • CiviCRM version: 4.5.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.27
  • PHP version: 5.3.13
Re: Need A Developer for a Custom Token
February 27, 2013, 01:00:55 pm
Thx, Coleman! I know it has been a long time to repond, sorry. I've been working on lots of configuration stuff. Can you tell me where, in what directory, I should put the new token code? Will the new tokens show up in the drop down menu for tokens during the mail creation process (like other tokens), or will I place html for new tokens manually into a document?
John Richardson

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Need A Developer for a Custom Token
February 27, 2013, 01:37:36 pm
In wordpress the easiest thing is to place it in your theme's functions.php file. And instead of calling the functions hook_civicrm_tokens and hook_civicrm_tokenValues you would rename them wordpress_civicrm_tokens wordpress_civicrm_tokenValues.
Note that hooks can only be defined once, but you can put as much code as you want in them, so you notice that in the blog article there are 3 code examples and they all declare hook_civicrm_tokenValues. In real life you would only name the function once and all the code would go in there.
And yes, adding new tokens will cause them to show up in the list.
If this is your first time working with hooks I recommend putting some debug statements into the code so you can see the values being passed into the functions and understand what's going on - much less like shooting in the dark that way.
Try asking your question on the new CiviCRM help site.

John Richardson

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 0
    • Life Challenge of Southeastern Michigan
  • CiviCRM version: 4.5.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.27
  • PHP version: 5.3.13
Re: Need A Developer for a Custom Token
February 27, 2013, 07:13:13 pm
I should have mentioned that I have converted my site to Drupal. Actually, I am using WP on my main site and have the civicrm installed in Drupal site in a subdomain. I felt like I was missing too many features of civicrm in WP. Anyway, where should the hooks go in a Drupal installation of civicrm. Thanks for your help!
John Richardson

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Need A Developer for a Custom Token
February 28, 2013, 05:42:21 am
Quote from: ideaman on February 27, 2013, 07:13:13 pm
. Anyway, where should the hooks go in a Drupal installation of civicrm. Thanks for your help!

You make a module as per: http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification#CiviCRMhookspecification-Proceduresforimplementinghooks%28forDrupal%29
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

John Richardson

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 0
    • Life Challenge of Southeastern Michigan
  • CiviCRM version: 4.5.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.27
  • PHP version: 5.3.13
Re: Need A Developer for a Custom Token
February 28, 2013, 09:07:18 am
Thanks Hershel. I am going to try it. And thanks for watching out for me on the forums. I flail and bob around, but my head is still above water. I'll let you know how it goes.  :)
John Richardson

John Richardson

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 0
    • Life Challenge of Southeastern Michigan
  • CiviCRM version: 4.5.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.27
  • PHP version: 5.3.13
Re: Need A Developer for a Custom Token
February 28, 2013, 12:18:09 pm
Okay, I tried it and failed. I created a Ne directory called civiNicknameToken in: /home/lifechal/www/data/sites/all/modules. In it I put a .info file and a .module file, both named civiNicknameToken. Here is the code for the module:
Code: [Select]
<?php
/**
implements hook_tokenValues
*/

function civiNicknameToken_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'];
      }
    }
  }
}

And here is the code fore the .info:

Code: [Select]
name = Smart Nickname
description = Creates token to default to first_name if nick_name is missing, or "friend" if no name for contact.
dependencies[] = civicrm
package = CiviCRM
core = 7.x
version = 1.0

I don't fine this token in the dropdown lists of Tokens available. You mentioned earlier that I am getting a js ckeditor error somewhere. Could that be why? the module shows up as exxpected in the module admin area and it is enabled...thoughts?
John Richardson

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • Need A Developer for a Custom Token

This forum was archived on 2017-11-26.