You are here

public function TokenReplacer::replaceUserDefined in Menu Token 8

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

Replace token string with the value from context.

Parameters

string $token: Token to be replaced.

string $key: Key in token.

string $value: Admin submited value.

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

Return value

array|string Returns replaced token.

File

src/Service/TokenReplacer.php, line 184

Class

TokenReplacer
TokenReplacer class.

Namespace

Drupal\menu_token\Service

Code

public function replaceUserDefined($token, $key, $value, BubbleableMetadata $b) {
  $token_type = $this
    ->getTokenType($token);
  $entity_type = $this->tokenEntityMapper
    ->getEntityTypeForTokenType($token_type);
  $entity_object = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($value);
  $replacement = $this->tokenService
    ->generate($token_type, [
    $key => $token,
  ], [
    $token_type => $entity_object,
  ], [], $b);
  return $replacement;
}