3 trait CRM_Core_TokenTrait
{
6 private $customFieldTokens;
9 * CRM_Entity_Tokens constructor.
11 public function __construct() {
12 parent
::__construct($this->getEntityName(), array_merge(
13 $this->getBasicTokens(),
14 $this->getCustomFieldTokens()
21 public function checkActive(\Civi\Token\TokenProcessor
$processor) {
22 return in_array($this->getEntityIDFieldName(), $processor->context
['schema'], TRUE) ||
23 (!empty($processor->context
['actionMapping'])
24 && $processor->context
['actionMapping']->getEntity() === $this->getEntityTableName());
30 public function getActiveTokens(\Civi\Token\Event\TokenValueEvent
$e) {
31 $messageTokens = $e->getTokenProcessor()->getMessageTokens();
32 if (!isset($messageTokens[$this->entity
])) {
37 // if message token contains '_\d+_', then treat as '_N_'
38 foreach ($messageTokens[$this->entity
] as $msgToken) {
39 if (array_key_exists($msgToken, $this->tokenNames
)) {
40 $activeTokens[] = $msgToken;
43 $altToken = preg_replace('/_\d+_/', '_N_', $msgToken);
44 if (array_key_exists($altToken, $this->tokenNames
)) {
45 $activeTokens[] = $msgToken;
49 return array_unique($activeTokens);
53 * Find the fields that we need to get to construct the tokens requested.
55 * @param array $tokens list of tokens
56 * @return array list of fields needed to generate those tokens
58 public function getReturnFields($tokens) {
59 // Make sure we always return something
62 foreach (array_intersect($tokens,
63 array_merge(array_keys(self
::getBasicTokens()), array_keys(self
::getCustomFieldTokens()))
65 if (isset(self
::$fieldMapping[$token])) {
66 $fields = array_merge($fields, self
::$fieldMapping[$token]);
72 return array_unique($fields);
76 * Get the tokens for custom fields
77 * @return array token name => token label
79 protected function getCustomFieldTokens() {
80 if (!isset($this->customFieldTokens
)) {
81 $this->customFieldTokens
= \CRM_Utils_Token
::getCustomFieldTokens(ucfirst($this->getEntityName()));
83 return $this->customFieldTokens
;