function cas_attributes_tokens in CAS Attributes 8
Same name and namespace in other branches
- 7 cas_attributes.tokens.inc \cas_attributes_tokens()
- 2.x cas_attributes.tokens.inc \cas_attributes_tokens()
Implements hook_tokens().
File
- ./
cas_attributes.tokens.inc, line 26 - Token module integration.
Code
function cas_attributes_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$sanitize = !empty($options['sanitize']);
$replacements = [];
$token_service = \Drupal::token();
if ($type == 'cas') {
// Find all attribute tokens formatted like [cas:attribute:?].
if ($attribute_tokens = $token_service
->findWithPrefix($tokens, 'attribute')) {
$casAttributes = [];
// Attributes may have been explicitly passed into the token replacer,
// so check for them there first. Otherwise, fallback to checking the
// session data. The main CAS module stores all attributes in the session
// when login is finalized.
if (!empty($data['cas_attributes'])) {
$casAttributes = $data['cas_attributes'];
}
else {
$session = \Drupal::request()
->getSession();
if ($session
->has('cas_attributes')) {
$casAttributes = $session
->get('cas_attributes');
}
}
// Change attribute names to lower case. All other case of token names
// in Drupal is lowercase, so this allows users to keep with that
// same convention.
$casAttributes = array_change_key_case($casAttributes, CASE_LOWER);
foreach ($attribute_tokens as $attribute => $originalToken) {
// All attributes in CAS are stored as arrays, even though in most cases
// they will they have one item. If the user did not provide a specific
// array modifier to the token, assume that we should just join all
// items of the array into a single string.
// See "Array tokens." in token.tokens.inc.
if (strpos($attribute, ':') === FALSE) {
$attribute .= ':join';
}
// Extract the array token from the end of the attribute token.
list($attribute, $arrayToken) = explode(':', $attribute, 2);
$attribute = mb_strtolower($attribute);
if (isset($casAttributes[$attribute])) {
$casAttribute = $casAttributes[$attribute];
if (!is_array($casAttribute)) {
$casAttribute = [
$casAttribute,
];
}
// Invoke the token generator to provide a replacement for the array
// token.
$replacements += $token_service
->generate('array', [
$arrayToken => $originalToken,
], [
'array' => $casAttribute,
], $options, $bubbleable_metadata);
}
elseif ($attribute == '?') {
// If the question mark token was used, then just output information
// about all available attributes.
$keys = array_keys($casAttributes);
if ($sanitize) {
$keys = array_map([
'SafeMarkup',
'checkPlain',
], $keys);
}
$replacements[$originalToken] = t('Available attributes: %keys', [
'%keys' => implode(', ', $keys),
]);
}
}
}
}
return $replacements;
}