Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hookTokens to be clearable outside the class #21836

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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