View source
<?php
namespace Drupal\menu_token\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\token\TokenEntityMapperInterface;
use Drupal\token\TokenInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
class TokenReplacer {
protected $tokenService;
protected $contextRepository;
protected $tokenEntityMapper;
protected $entityTypeManager;
protected $url;
public function __construct(TokenInterface $tokenService, ContextRepositoryInterface $c, TokenEntityMapperInterface $tem, EntityTypeManagerInterface $en) {
$this->tokenService = $tokenService;
$this->contextRepository = $c;
$this->tokenEntityMapper = $tem;
$this->entityTypeManager = $en;
}
private function getTokenType($token) {
preg_match_all('/
\\[ # [ - pattern start
([^\\s\\[\\]:]+) # match $type not containing whitespace : [ or ]
: # : - separator
([^\\[\\]]+) # match $name not containing [ or ]
\\] # ] - pattern end
/x', $token, $matches);
$types = $matches[1];
return $types[0];
}
public function replaceNone($token) {
$replacement = [
$token => '',
];
return $replacement;
}
public function replaceContext($token, $key, BubbleableMetadata $b) {
$token_type = $this
->getTokenType($token);
$entity_type = $this->tokenEntityMapper
->getEntityTypeForTokenType($token_type);
$b
->addCacheContexts([
"url",
]);
$b
->addCacheContexts([
"user",
]);
if ($entity_type === FALSE) {
return "";
}
$contexts_def = $this->contextRepository
->getAvailableContexts();
$real_context = $this->contextRepository
->getRuntimeContexts(array_keys($contexts_def));
foreach ($real_context as $key_i => $real_ci) {
if (!$real_ci
->hasContextValue()) {
continue;
}
$context_data_definition_type = $real_ci
->getContextData()
->getPluginDefinition();
$value = $real_ci
->getContextData()
->getValue();
if ($entity_type == "user" && method_exists($value, "isAnonymous") && $value
->isAnonymous()) {
return [
$token => "Anonymous",
];
}
if (empty($value)) {
switch ($entity_type) {
case "user":
$value = User::load(\Drupal::currentUser()
->id());
break;
default:
break;
}
}
if ($context_data_definition_type["id"] == "entity" && method_exists($value, "getEntityTypeId") && $value
->getEntityTypeId() == $entity_type) {
if (!empty($value)) {
$r_var = $value;
if (is_array($r_var)) {
$r_var = array_pop($r_var);
}
$replacement = $this->tokenService
->generate($token_type, [
$key => $token,
], [
$token_type => $r_var,
], [], $b);
return $replacement;
}
}
}
return "";
}
public function replaceRandom($token, $key, BubbleableMetadata $b) {
$token_type = $this
->getTokenType($token);
$entity_type = $this->tokenEntityMapper
->getEntityTypeForTokenType($token_type);
$query = $this->entityTypeManager
->getStorage($entity_type)
->getQuery("AND");
$user_ids = $query
->execute();
$random_id = array_rand($user_ids, 1);
$random_user = $this->entityTypeManager
->getStorage($entity_type)
->load($random_id);
$replacement = $this->tokenService
->generate($token_type, [
$key => $token,
], [
$token_type => $random_user,
], [], $b);
return $replacement;
}
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;
}
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;
}
$replacement = $this->tokenService
->generate($token_type, [
$key => $token,
], $data, [], $b);
return $replacement;
}
}