You are here

function sms_tokens in SMS Framework 2.x

Same name and namespace in other branches
  1. 8 sms.tokens.inc \sms_tokens()
  2. 7 sms.tokens.inc \sms_tokens()
  3. 2.1.x sms.tokens.inc \sms_tokens()

Implements hook_tokens().

File

./sms.tokens.inc, line 50
Token callbacks for the sms module.

Code

function sms_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'sms') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'verification-url':
          $url_options = [
            'absolute' => TRUE,
          ];
          $result = Url::fromRoute('sms.phone.verify', [], $url_options)
            ->toString(TRUE);
          assert($result instanceof GeneratedUrl);
          $replacements[$original] = $result
            ->getGeneratedUrl();
          break;
      }
    }
  }
  if ($type == 'sms-message') {
    if (!empty($data['sms-message']) && $data['sms-message'] instanceof SmsMessageInterface) {

      /** @var \Drupal\sms\Message\SmsMessageInterface $sms_message */
      $sms_message = $data['sms-message'];
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'phone-number':
            $recipients = $sms_message
              ->getRecipients();
            $replacements[$original] = reset($recipients) ?: '';
            break;
          case 'message':
            $replacements[$original] = $sms_message
              ->getMessage();
            break;
        }
      }
    }
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'verification-code':
          if (!empty($data['sms_verification_code'])) {
            $replacements[$original] = $data['sms_verification_code'];
          }
          break;
      }
    }
  }
  return $replacements;
}