You are here

public function EmailTokenEvaluator::replaceUnsafeTokens in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x src/Service/EmailTokenEvaluator.php \Drupal\easy_email\Service\EmailTokenEvaluator::replaceUnsafeTokens()

@inheritDoc

Overrides EmailTokenEvaluatorInterface::replaceUnsafeTokens

File

src/Service/EmailTokenEvaluator.php, line 124

Class

EmailTokenEvaluator

Namespace

Drupal\easy_email\Service

Code

public function replaceUnsafeTokens($text, AccountInterface $recipient) {
  $unsafe_tokens = $this
    ->unsafeTokens();
  $tokens = $this->token
    ->scan($text);
  if (!empty($tokens['easy_email'])) {
    foreach ($tokens['easy_email'] as $token => $full_token) {
      $token_parts = explode(':', $token);
      $final_token = array_pop($token_parts);
      if (in_array($final_token, $unsafe_tokens)) {
        $text = str_replace($full_token, '[user:' . $final_token . ']', $text);
      }
    }
  }
  return $this->token
    ->replace($text, [
    'user' => $recipient,
  ], [
    'callback' => 'user_mail_tokens',
  ]);
}