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) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 4.1 Release Testing (Moderator: Kurund Jalmi) »
  • Hook_tokenValues called once for each contact
Pages: [1]

Author Topic: Hook_tokenValues called once for each contact  (Read 1952 times)

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Hook_tokenValues called once for each contact
January 10, 2012, 08:11:37 am
I'm excited about the new hook_tokenValues and being able to write some great efficient queries. One problem:
When I test using print pdf letter, instead of calling the hook once and giving each contact ID in an array, it calls the hook once per contact (with the $cids array containing just the one cid) which pretty much defeats the point of writing a nice query.
Try asking your question on the new CiviCRM help site.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Hook_tokenValues called once for each contact
January 10, 2012, 09:24:04 am

we've only optimized civimail to do token calls in batch. Most of the others are done on a per-contact basis

However with generating PDF's, i think the bottle-neck is still the pdf generation, so optimizing queries is probably not the biggest bang for your buck in this case (IMO). If you wanna take a look at optimizing this stuff, check:

CRM/Contact/Form/Task/PDFLetterCommon.php, line 265

to do the token hook in batch should be quite easy. I would do a loop of loops (with a batch count) in case u have an excessively large number of ids to process)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Hook_tokenValues called once for each contact
January 10, 2012, 05:29:04 pm
Good to know. I hope to work on that sometime. Optimizing it would be cool. I remember Eileen noted that she had to give up on PDF letters because it choked on just a few hundred contacts.

One more odd thing I noticed:

the hook_tokens() requires redundant names, like
$tokens['contact']['contact.address_format'] = 'Address Formatted'; // notice the unnecessary 'contact.' prefix

but the $tokens param sent in to hook_tokenValues then shows it as
$tokens['contact']['address_format'] // no redundant dot thingey

but to fill its value in hook_tokenValues, the dot thingey is needed:
$values[$cid]['contact.address_format'] = 'foo';

despite the fact that it's not there in existing values. Example of what $values looks like after I just added that value:
array(
  'contact_id' => 123
  'display_name' => 'Foo Bar',
  'contact.address_format' => 'foo',
);

Oddly inconsistent. but when I try just adding 'address_format' it don't work.
« Last Edit: January 10, 2012, 05:31:21 pm by colemanw »
Try asking your question on the new CiviCRM help site.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Hook_tokenValues called once for each contact
January 11, 2012, 03:08:27 pm

yes it is definitely inconsistent :( too bad we did not catch it when we did the 4.1 round of cleanups.

Any interest in cleaning that part of the code for 4.2?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Hook_tokenValues called once for each contact
January 11, 2012, 05:09:05 pm
Interest, yes. Time, maybe. And the trouble with fixing hooks is that it also breaks them, for anyone who's already implemented it the "wrong" way.
Try asking your question on the new CiviCRM help site.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Hook_tokenValues called once for each contact
January 15, 2012, 06:47:13 am
I've taken a look at the code and I may be able to supply a patch to optimize token replacements. But I was surprised to see how "ad hoc" the replacements seem to be -- that is, a lot seems to be happening within the CRM/Contact/Form/Task/PDFLetterCommon.php file (such as calling the hooks) that could probably be moved to a more central place to avoid having to do it every time we generate something with tokens.
Does that sound right to you?
Try asking your question on the new CiviCRM help site.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Hook_tokenValues called once for each contact
January 15, 2012, 07:27:42 am

yes, centralizing the token code in one place would also be good.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 4.1 Release Testing (Moderator: Kurund Jalmi) »
  • Hook_tokenValues called once for each contact

This forum was archived on 2017-11-26.