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

(REF) Allow subclasses of AbstractTokenSubscriber to override the determination of active tokens. #13186

Merged
merged 1 commit into from
Dec 1, 2018
Merged
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
23 changes: 18 additions & 5 deletions Civi/Token/AbstractTokenSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* 3. Implement the evaluateToken() method.
* 4. Optionally, override others:
* + checkActive()
* + getActiveTokens()
* + prefetch()
* + alterActionScheduleMailing()
* 5. Register the new class with the event-dispatcher.
Expand Down Expand Up @@ -142,13 +143,10 @@ public function evaluateTokens(TokenValueEvent $e) {
return;
}

$messageTokens = $e->getTokenProcessor()->getMessageTokens();
if (!isset($messageTokens[$this->entity])) {
$activeTokens = $this->getActiveTokens($e);
if (!$activeTokens) {
return;
}

$activeTokens = array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames));

$prefetch = $this->prefetch($e);

foreach ($e->getRows() as $row) {
Expand All @@ -158,6 +156,21 @@ public function evaluateTokens(TokenValueEvent $e) {
}
}

/**
* To handle variable tokens, override this function and return the active tokens.
*
* @param \Civi\Token\Event\TokenValueEvent $e
*
* @return mixed
*/
public function getActiveTokens(TokenValueEvent $e) {
$messageTokens = $e->getTokenProcessor()->getMessageTokens();
if (!isset($messageTokens[$this->entity])) {
return FALSE;
}
return array_intersect($messageTokens[$this->entity], array_keys($this->tokenNames));
}

/**
* To perform a bulk lookup before rendering tokens, override this
* function and return the prefetched data.
Expand Down