4 use Civi\Token\Event\TokenRenderEvent
;
5 use Civi\Token\Event\TokenValueEvent
;
6 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
9 * Class TokenCompatSubscriber
12 * This class provides a compatibility layer for using CRM_Utils_Token
13 * helpers within TokenProcessor.
15 * THIS IS NOT A GOOD EXAMPLE TO EMULATE. The class exists to two
16 * bridge two different designs. CRM_Utils_Token has some
17 * undesirable elements (like iterative token substitution).
18 * However, if you're refactor CRM_Utils_Token or improve the
19 * bridge, then it makes sense to update this class.
21 class TokenCompatSubscriber
implements EventSubscriberInterface
{
26 public static function getSubscribedEvents() {
28 'civi.token.eval' => 'onEvaluate',
29 'civi.token.render' => 'onRender',
36 * @param \Civi\Token\Event\TokenValueEvent $e
37 * @throws TokenException
39 public function onEvaluate(TokenValueEvent
$e) {
40 // For reasons unknown, replaceHookTokens requires a pre-computed list of
41 // hook *categories* (aka entities aka namespaces). We'll cache
42 // this in the TokenProcessor's context.
45 \CRM_Utils_Hook
::tokens($hookTokens);
46 $categories = array_keys($hookTokens);
47 $e->getTokenProcessor()->context
['hookTokenCategories'] = $categories;
49 $messageTokens = $e->getTokenProcessor()->getMessageTokens();
51 foreach ($e->getRows() as $row) {
52 if (empty($row->context
['contactId'])) {
55 /** @var int $contactId */
56 $contactId = $row->context
['contactId'];
57 if (empty($row->context
['contact'])) {
59 ['contact_id', '=', $contactId, 0, 0],
61 [$contact] = \CRM_Contact_BAO_Query
::apiQuery($params);
63 $contact = reset($contact);
64 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
65 // FIXME: Need to differentiate errors which kill the batch vs the individual row.
66 \Civi
::log()->debug("Failed to generate token data. Invalid contact ID: " . $row->context
['contactId']);
70 //update value of custom field token
71 if (!empty($messageTokens['contact'])) {
72 foreach ($messageTokens['contact'] as $token) {
73 if (\CRM_Core_BAO_CustomField
::getKeyID($token)) {
74 $contact[$token] = civicrm_api3('Contact', 'getvalue', [
83 $contact = $row->context
['contact'];
86 if (!empty($row->context
['tmpTokenParams'])) {
87 // merge activity tokens with contact array
88 // this is pretty weird.
89 $contact = array_merge($contact, $row->context
['tmpTokenParams']);
92 $contactArray = [$contactId => $contact];
93 \CRM_Utils_Hook
::tokenValues($contactArray,
95 empty($row->context
['mailingJobId']) ?
NULL : $row->context
['mailingJobId'],
97 $row->context
['controller']
100 // merge the custom tokens in the $contact array
101 if (!empty($contactArray[$contactId])) {
102 $contact = array_merge($contact, $contactArray[$contactId]);
104 $row->context('contact', $contact);
109 * Apply the various CRM_Utils_Token helpers.
111 * @param \Civi\Token\Event\TokenRenderEvent $e
113 public function onRender(TokenRenderEvent
$e) {
114 $isHtml = ($e->message
['format'] == 'text/html');
115 $useSmarty = !empty($e->context
['smarty']);
117 $domain = \CRM_Core_BAO_Domain
::getDomain();
118 $e->string = \CRM_Utils_Token
::replaceDomainTokens($e->string, $domain, $isHtml, $e->message
['tokens'], $useSmarty);
120 if (!empty($e->context
['contact'])) {
121 \CRM_Utils_Token
::replaceGreetingTokens($e->string, $e->context
['contact'], $e->context
['contact']['contact_id'], NULL, $useSmarty);
122 $e->string = \CRM_Utils_Token
::replaceContactTokens($e->string, $e->context
['contact'], $isHtml, $e->message
['tokens'], TRUE, $useSmarty);
124 // FIXME: This may depend on $contact being merged with hook values.
125 $e->string = \CRM_Utils_Token
::replaceHookTokens($e->string, $e->context
['contact'], $e->context
['hookTokenCategories'], $isHtml, $useSmarty);
129 $smarty = \CRM_Core_Smarty
::singleton();
130 $e->string = $smarty->fetch("string:" . $e->string);