Skip to content

Commit

Permalink
Merge pull request #13186 from aydun/override_active_tokens
Browse files Browse the repository at this point in the history
(REF) Allow subclasses of AbstractTokenSubscriber to override the determination of active tokens.
  • Loading branch information
eileenmcnaughton authored Dec 1, 2018
2 parents 762e3fc + eb96909 commit 243eec2
Showing 1 changed file with 18 additions and 5 deletions.
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

0 comments on commit 243eec2

Please sign in to comment.