/** * Load token data. * * @param TokenValueEvent $e * @throws TokenException */ public function onEvaluate(TokenValueEvent $e) { // For reasons unknown, replaceHookTokens requires a pre-computed list of // hook *categories* (aka entities aka namespaces). We'll cache // this in the TokenProcessor's context. $hookTokens = array(); \CRM_Utils_Hook::tokens($hookTokens); $categories = array_keys($hookTokens); $e->getTokenProcessor()->context['hookTokenCategories'] = $categories; $messageTokens = $e->getTokenProcessor()->getMessageTokens(); foreach ($e->getRows() as $row) { if (empty($row->context['contact'])) { $params = array(array('contact_id', '=', $row->context['contactId'], 0, 0)); list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params); $contact = reset($contact); //CRM-4524 if (!$contact || is_a($contact, 'CRM_Core_Error')) { // FIXME: Need to differentiate errors which kill the batch vs the individual row. throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); } } else { $contact = $row->context['contact']; } if (!empty($row->context['tmpTokenParams'])) { // merge activity tokens with contact array // this is pretty weird. $contact = array_merge($contact, $row->context['tmpTokenParams']); } // Note: This is a small contract change from the past; data should be missing // less randomly. //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']); \CRM_Utils_Hook::tokenValues($contact, $row->context['contactId'], empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id, $messageTokens, $row->context['controller']); $row->context('contact', $contact); } }
/** * Populate the token data. * * @param TokenValueEvent $e * The event, which includes a list of rows and tokens. */ public function evaluateTokens(TokenValueEvent $e) { if (!$this->checkActive($e->getTokenProcessor())) { return; } // TODO: check if any tokens for $entity are actually used; short-circuit. $prefetch = $this->prefetch($e); foreach ($e->getRows() as $row) { foreach ($this->tokenNames as $field => $label) { $this->evaluateToken($row, $this->entity, $field, $prefetch); } } }
public function onEvalTokens(TokenValueEvent $e) { $this->counts[__FUNCTION__]++; foreach ($e->getRows() as $row) { /** @var TokenRow $row */ $row->format('text/html'); $row->tokens['custom']['foobar'] = sprintf("#%04d is a good number. Trickster {contact.display_name}.", $row->context['contact_id']); } }