TokenProcessor - Allow defining Smarty variables which are opulated from tokens
authorTim Otten <totten@civicrm.org>
Wed, 1 Sep 2021 04:21:41 +0000 (21:21 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 1 Sep 2021 06:17:31 +0000 (23:17 -0700)
commit8996a8b6d7f61021373b3cb89c72dc6670cf202e
tree68febb81923fd380f6ecfa791b179d78f8a5ac10
parenta7a555616f41fe6c94f9f34784a9f8def28e1fc0
TokenProcessor - Allow defining Smarty variables which are opulated from tokens

Overview
--------

This allow more interoperability between Smarty expressions and tokens.  For
example, suppose one had a contribution-related message that could use the
Smarty variable `$theInvoiceId` and/or the token `{contribution.invoice_id}`.
This revision allows the Smarty variable to function as an alias for the token.

Before
------

The caller would need to precompute Smarty values, eg

```php
$theInvoiceId = civicrm_api4('Contribution', 'get', [
  'select' => 'invoice_id',
  'where' => [['id', '=', $contributionId]]
]);
$p = new TokenProcessor($this->dispatcher, [
  'controller' => __CLASS__,
  'schema' => ['contributionId'],
  'smarty' => TRUE,
]);
$p->addMessage('example', 'Invoice #{$theInvoiceId}!', 'text/plain');
$p->addRow(['contributionId' => 123]);
```

After
-----

The caller can declare a Smarty=>Token alias and leverage token data-loader.

```php
$p = new TokenProcessor($this->dispatcher, [
  'controller' => __CLASS__,
  'schema' => ['contributionId'],
  'smarty' => TRUE,
  'smartyTokenAlias' => [
    'theInvoiceId' => 'contribution.invoice_id',
  ],
]);
$p->addMessage('example', 'Invoice #{$theInvoiceId}!', 'text/plain');
$p->addRow(['contributionId' => 123]);
```

Comments
--------

The target token must be populated via `civi.token.eval` (e.g `$e->token('foo', 'bar', 'value')`).
This would work with `CRM_*_Tokens`.  But if the token is evaluted by other means (eg
`CRM_Utils_Token::replaceGreetingTokens()`), then it won't currently be resolved.
Civi/Token/TokenCompatSubscriber.php
Civi/Token/TokenProcessor.php
tests/phpunit/Civi/Token/TokenProcessorTest.php