cas-8.x-1.x-dev/cas.tokens.inc

cas.tokens.inc
<?php

/**
 * @file
 * Token module integration.
 */

declare(strict_types=1);

use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;

/**
 * Implements hook_token_info().
 */
function cas_token_info(): array {
  return [
    'types' => [
      'cas' => [
        'name' => t('CAS'),
        'description' => t('Tokens related to the CAS module.'),
      ],
    ],
    'tokens' => [
      'cas' => [
        'username' => [
          'name' => t('Username'),
          'description' => t('On most sites this is the same as the Drupal username.'),
        ],
        'login-url' => [
          'name' => t('Login URL (relative)'),
          'description' => t('The CAS login URL as a relative path.'),
        ],
        'login-url-absolute' => [
          'name' => t('Login URL (absolute)'),
          'description' => t('The complete CAS login URL including protocol and hostname,'),
        ],
      ],
    ],
  ];
}

/**
 * Implements hook_tokens().
 */
function cas_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata): array {
  $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;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc