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) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Custom Token
Pages: [1]

Author Topic: Custom Token  (Read 517 times)

mvisser

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: CiviCRM 4.4.6
  • CMS version: Joomla! 2.5.17
  • MySQL version: 5.5.38
  • PHP version: 5.4.29
Custom Token
December 08, 2014, 04:22:22 am
Hi all,

I am trying to implement a custom token through a plugin in Joomla.

To test the implementation of a hook I used the following example :

function hook_civicrm_tabs( &$tabs, $contactID ) {
 
    // unset the contribition tab, i.e. remove it from the page
    unset( $tabs[1] );
 
    // let's add a new "contribution" tab with a different name and put it last
    // this is just a demo, in the real world, you would create a url which would
    // return an html snippet etc.
    $url = CRM_Utils_System::url( 'civicrm/contact/view/contribution',
                                  "reset=1&snippet=1&force=1&cid=$contactID" );
    $tabs[] = array( 'id'    => 'mySupercoolTab',
                     'url'   => $url,
                     'title' => 'Contribution Tab Renamed',
                     'weight' => 300 );
}

This worked fine !!!!.

When I try to implement a hook to create a custom token I can't get it to work.

Here is the code:
<?php 
# implement the tokens hook so we can add our new token to the list of tokens
# displayed to CiviMail users

#file_put_contents('/shares/data/html/civicus.test/logs/hook_debug_civicrm.txt', "Entered hook code!\n", FILE_APPEND | LOCK_EX);

defined('_JEXEC') or die;
jimport('cms.plugin.plugin');

class plgCiviCRMMyPlugin extends JPlugin {

  function hook_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 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;
       }
    }
  }
}

____________________________________________________________________________________
<?xml version="1.0" encoding="UTF-8"?>

<extension version="1.1" type="plugin" group="civicrm">
<name>plg_civcrm_tokens</name>
<author>Joomla! Project</author>
<creationDate>November 2014</creationDate>
<copyright>Copyright (C) 2014 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>1.0.0</version>

<description>PLG_CIVICRM_TOKENS_XML_DESCRIPTION</description>
<files>
<filename plugin="token">tokens.php</filename>
<filename>index.html</filename>
<folder>language</folder>
</files>
<config>
</config>
</extension>

The plugin is correctly installed, but it seems the functions never gets triggered.

Thanks


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 Token
December 09, 2014, 01:31:25 am
In some cases such as sending bulk mails the parameters are different. I have noticed that the parameters $cids is not always an array, it could also be a single contact id.

Also the parameters $tokens could be defined as $tokens['date'] or as $tokens[] = 'date'

This is very strange behaviour of the token system
Developer at Edeveloper / CiviCoop

mvisser

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: CiviCRM 4.4.6
  • CMS version: Joomla! 2.5.17
  • MySQL version: 5.5.38
  • PHP version: 5.4.29
Re: Custom Token
December 09, 2014, 06:04:38 am
Hi Jaap,

Thank you for the reply.

How can I test to see if the function is called?

Once I know the function is active, we can start looking at the parameters being passed.


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 Token
December 09, 2014, 06:13:59 am
You can test the function to send an e-mail with tokens (and your token) or send pdf letter to a contact with the token.
Developer at Edeveloper / CiviCoop

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Custom Token

This forum was archived on 2017-11-26.