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 (Moderator: Donald Lobo) »
  • Plugable tokens
Pages: [1]

Author Topic: Plugable tokens  (Read 1462 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Plugable tokens
March 14, 2012, 05:48:54 pm
One problem I hit with custom tokens is that they aren't very portable - if you want to re-use some (but not all) tokens from one site on another (which has it's own tokens) you have to sort of integrate the tokens from the module on the first site into the ones on the second site - or split them out into lots of separate modules.

I have created my own civitoken module (https://svn.fuzion.co.nz/repos/fuzion-code/trunk/drupal/d7modules/civitoken) which looks in the custom php directory for a folder called 'tokens' & loads token files from there. This makes the token definition very granular & easy to move individual token files from one site to another.


Here's a sample - it exposes the name of a related contact as the mygroup.group token. I can put this in the $custom_php_dir/token folder on any site that has civitoken installed & it will become available on that site - of course this not an example of a token that I would port to other sites.

 mygroup.inc

Code: [Select]
function mygroup_civitoken_declare($token){
  return array(
    $token. '.group' => 'My Group',
    );
}


function abagroup_civitoken_get($cid, &$value){
  //Get contacts group
  $group = civicrm_api('relationship', 'get', array(
    'version' => 3,
    'contact_id_a' => $cid,
    'relationship_type_id' => 13,
    'sequential' => 1,
    'api.contact.getvalue' => array(
      'id' => '$value.contact_id_b',
      'return' => 'display_name')));
 
 

    $value['mygroup.group'] = $group['values'][0]['api.contact.getvalue'];
return $value;
}
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Plugable tokens
April 13, 2012, 10:51:57 pm
Sounds a useful addition. Shouldn't it be part of the core?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Plugable tokens
April 13, 2012, 11:32:17 pm
yes, someday.

For now it's just a drupal module
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Plugable tokens
April 14, 2012, 12:13:24 am
Couldn't it be a CMS agnostic extension?

(and part of the totten tool)

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Plugable tokens
April 14, 2012, 12:23:57 am
Yep - think you can talk Tim into doing it?
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Plugable tokens
April 14, 2012, 04:01:23 pm
Nice one Eileen!
One suggestion: pass $cids (as an array) instead of $cid to allow the possibility of an efficient select query that fetches data all at once.
Also, is your module smart enough to only call for data if a token is actually in use? I assume yes, but if no it's an easy fix.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Plugable tokens
April 14, 2012, 04:13:04 pm
Hmm Coleman - that possibility just never arose for me (the efficient query :-)). I think it would make sense to to have a different function name for that.

I don't think it's doing #2 because I have been  using it against 4.0 & I think #2 only become doable for 4.1 & I haven't gotten my head around the changes yet.
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Plugable tokens
April 14, 2012, 04:18:11 pm
Here's some examples for 4.1:
http://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit
In 4.1, contact ID has been standardized to always be an array (even if only contains one value). So no more messing around with "if ($single)" junk. And tokens in use are declared, so you do not have to fetch data for everything, only what's needed.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Plugable tokens
April 14, 2012, 04:34:08 pm
Ah yes, that is the obvious answer.

If anyone else has any interest in extending this before it hits the top of my priority list I'm happy for someone to move it onto github (or even if they feel ready drupal.org)
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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Plugable tokens

This forum was archived on 2017-11-26.