Skip to content

Commit

Permalink
Merge pull request #21836 from eileenmcnaughton/hook_toke
Browse files Browse the repository at this point in the history
Fix hookTokens to be clearable outside the class
  • Loading branch information
totten authored Oct 15, 2021
2 parents 0f0fc39 + 15ed06b commit 5b6ef63
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
25 changes: 7 additions & 18 deletions CRM/Contact/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ protected function getApiEntityName(): string {
return 'Contact';
}

/**
* Tokens defined by the legacy hook.
*
* @var array
*/
protected $hookTokens;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -249,7 +242,7 @@ protected function getRelatedEntityTokenMetadata(): array {
*/
public function evaluateLegacyHookTokens(TokenValueEvent $e): void {
$messageTokens = $e->getTokenProcessor()->getMessageTokens();
if (!array_intersect(array_keys($this->getHookTokens()), array_keys($messageTokens))) {
if (empty($messageTokens) || !array_intersect(array_keys($this->getHookTokens()), array_keys($messageTokens))) {
return;
}

Expand Down Expand Up @@ -694,17 +687,13 @@ protected function getBespokeTokens(): array {
* @return array
*/
protected function getHookTokens(): array {
if ($this->hookTokens === NULL) {
if (isset(Civi::$statics[__CLASS__]['hook_tokens'])) {
$this->hookTokens = Civi::$statics[__CLASS__]['hook_tokens'];
}
else {
$this->hookTokens = [];
\CRM_Utils_Hook::tokens($this->hookTokens);
Civi::$statics[__CLASS__]['hook_tokens'] = $this->hookTokens;
}
if (isset(Civi::$statics[__CLASS__]['hook_tokens'])) {
return Civi::$statics[__CLASS__]['hook_tokens'];
}
return $this->hookTokens;
$tokens = [];
\CRM_Utils_Hook::tokens($tokens);
Civi::$statics[__CLASS__]['hook_tokens'] = $tokens;
return $tokens;
}

}
3 changes: 3 additions & 0 deletions CRM/Utils/Hook/UnitTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function setMock($mockObject): void {
*/
public function setHook(string $hook, $callable): void {
$this->adhocHooks[$hook] = $callable;
if (strpos($hook, 'token') !== FALSE) {
unset(Civi::$statics['CRM_Contact_Tokens']['hook_tokens']);
}
}

/**
Expand Down

0 comments on commit 5b6ef63

Please sign in to comment.