forked from bartfeenstra/drupal-currency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.tokens.inc
92 lines (81 loc) · 2.26 KB
/
currency.tokens.inc
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* @file
* Contains Token API hook implementations.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function currency_token_info() {
// Token groups.
$types['currency'] = array(
'name' => t('Currencies'),
'needs-data' => 'currency',
);
// Tokens.
$tokens['currency']['code'] = array(
'description' => t('The (ISO 4217) currency code.'),
'name' => t('Currency code'),
'type' => 'text',
);
$tokens['currency']['number'] = array(
'description' => t('The ISO 4217 currency number.'),
'name' => t('Currency number'),
'type' => 'text',
);
$tokens['currency']['subunits'] = array(
'description' => t('The number of subunits.'),
'name' => t('Subunits'),
'type' => 'text',
);
$tokens['currency']['sign'] = array(
'name' => t('Sign'),
'type' => 'text',
);
$tokens['currency']['title'] = array(
'name' => t('Name'),
'type' => 'text',
);
return array(
'tokens' => $tokens,
'types' => $types,
);
}
/**
* Implements hook_tokens().
*/
function currency_tokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
if ($type == 'currency' && isset($data['currency'])) {
$currency_code = $data['currency'];
if (isset($tokens['code'])) {
$replacements[$tokens['code']] = $currency_code;
unset($tokens['code']);
}
if ($tokens) {
/** @var \Drupal\currency\Entity\CurrencyInterface $currency */
$currency = \Drupal::entityManager()->getStorage('currency')->load($currency_code);
if ($currency) {
$bubbleable_metadata->addCacheableDependency($currency);
foreach ($tokens as $token => $original) {
switch ($token) {
case 'number':
$replacements[$original] = $currency->getCurrencyNumber();
break;
case 'sign':
$replacements[$original] = $currency->getSign();
break;
case 'subunits':
$replacements[$original] = $currency->getSubunits();
break;
case 'title':
$replacements[$original] = $currency->label();
break;
}
}
}
}
}
return $replacements;
}