You are here

function cas_tokens in CAS 8

Same name and namespace in other branches
  1. 7 cas.tokens.inc \cas_tokens()
  2. 2.x cas.tokens.inc \cas_tokens()

Implements hook_tokens().

File

./cas.tokens.inc, line 44
Token module integration.

Code

function cas_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'cas') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'username':
          if (array_key_exists('cas_username', $data)) {
            $replacements[$original] = $data['cas_username'];
          }
          else {

            // If it's not there, then check the session data instead.
            $session = \Drupal::request()
              ->getSession();
            if ($session
              ->has('cas_username')) {
              $replacements[$original] = $session
                ->get('cas_username');
            }
          }
          break;
        case 'login-url':
          $replacements[$original] = Url::fromRoute('cas.login')
            ->toString();
          break;
        case 'login-url-absolute':
          $replacements[$original] = Url::fromRoute('cas.login', [], [
            'absolute' => TRUE,
          ])
            ->toString();
          break;
      }
    }
  }
  return $replacements;
}