You are here

public function TokenReplacer::replaceExoticToken in Menu Token 9.1.x

Same name and namespace in other branches
  1. 8 src/Service/TokenReplacer.php \Drupal\menu_token\Service\TokenReplacer::replaceExoticToken()

Replace token string with the value from global and special tokens.

Parameters

string $token: Token to be replaced.

string $key: Key in token.

\Drupal\Core\Render\BubbleableMetadata $b: Bubable metadata for cache context.

Return value

array|string Returns replaced token.

File

src/Service/TokenReplacer.php, line 208

Class

TokenReplacer
TokenReplacer class.

Namespace

Drupal\menu_token\Service

Code

public function replaceExoticToken($token, $key, BubbleableMetadata $b) {
  $token_type = $this
    ->getTokenType($token);
  $b
    ->addCacheContexts([
    "url",
  ]);
  $b
    ->addCacheContexts([
    "user",
  ]);
  $data = [];
  switch ($token_type) {
    case "url":
      $data["url"] = Url::createFromRequest(\Drupal::request());
      break;
    case "current-user":
      $data["user"] = User::load(\Drupal::currentUser()
        ->id());
      if (method_exists($data["user"], "isAnonymous") && $data["user"]
        ->isAnonymous()) {
        return [
          $token => "Anonymous",
        ];
      }
      break;
    default:
      break;
  }

  // Exotic tokens...
  $replacement = $this->tokenService
    ->generate($token_type, [
    $key => $token,
  ], $data, [], $b);
  return $replacement;
}