function menu_token_tokens in Menu Token 8
Same name and namespace in other branches
- 9.1.x menu_token.module \menu_token_tokens()
Implements hook_tokens().
File
- ./
menu_token.module, line 316 - Contains menu_token.module.
Code
function menu_token_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
if (!empty($options['configuration'])) {
$configuration = $options['configuration'];
// Flag to know what to do with tokens where replacement is not found.
$remove_if_not_present = !empty($configuration['remove_if_replacement_is_not_present']) && $configuration['remove_if_replacement_is_not_present'] == 1;
$replacements = [];
foreach ($tokens as $key => $token) {
$entity_type = \Drupal::service('token.entity_mapper')
->getEntityTypeForTokenType($type);
$token_replacer = \Drupal::service('menu_token.token_replacer');
if (!empty($configuration[$entity_type][0])) {
// Make type agnostic so it can handle any type.
switch ($configuration[$entity_type][0]) {
case "none":
$r = $token_replacer
->replaceNone($token, $key, $bubbleable_metadata);
$replacements = array_merge($replacements, $r);
break;
case "context":
$r = $token_replacer
->replaceContext($token, $key, $bubbleable_metadata);
if (empty($r)) {
$r = $token_replacer
->replaceExoticToken($token, $key, $bubbleable_metadata);
if (empty($r)) {
$r = [
array_pop($tokens) => '',
];
}
}
$replacements = array_merge($replacements, $r);
break;
case "random":
$r = $token_replacer
->replaceRandom($token, $key, $bubbleable_metadata);
if (empty($r) && $remove_if_not_present) {
$r = [
array_pop($tokens) => '',
];
}
$replacements = array_merge($replacements, $r);
break;
case "user_defined":
$admin_defined_variable = $configuration[$type][1];
$r = $token_replacer
->replaceUserDefined($token, $key, $admin_defined_variable, $bubbleable_metadata);
if (empty($r) && $remove_if_not_present) {
$r = [
array_pop($tokens) => '',
];
}
$replacements = array_merge($replacements, $r);
break;
default:
break;
}
}
else {
$r = $token_replacer
->replaceExoticToken($token, $key, $bubbleable_metadata);
$replacements = array_merge($replacements, $r);
}
}
return $replacements;
}
}