This repository was archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_code.tokens.inc
More file actions
63 lines (51 loc) · 1.4 KB
/
magic_code.tokens.inc
File metadata and controls
63 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* @file
* Token definitions for magic_code.
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_token_info().
*/
function magic_code_token_info() {
$operations = _magic_code_get_available_tokens();
$tokens = [];
foreach ($operations as $operation) {
$key = 'magic-code-' . $operation;
$tokens[$key] = [
'name' => new TranslatableMarkup('Magic Code - @operation', [
'@operation' => $operation,
]),
'description' => new TranslatableMarkup('A magic code with the operation "@operation"', [
'@operation' => $operation,
]),
'restricted' => TRUE,
];
}
return [
'tokens' => [
'user' => $tokens,
],
];
}
/**
* Implements hook_tokens().
*/
function magic_code_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'user' && !empty($data['user'])) {
$operations = _magic_code_get_available_tokens();
foreach ($tokens as $name => $original) {
// Safety check.
if (!str_contains($name, 'magic-code-')) {
continue;
}
$operation = str_replace('magic-code-', '', $name);
if (in_array($operation, $operations)) {
$replacements[$original] = sprintf('[user:magic-code-%s_mail-only]', $operation);
}
}
}
return $replacements;
}